001package org.apache.maven.scm.provider.integrity.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.ScmFile;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.ScmResult;
026import org.apache.maven.scm.ScmVersion;
027import org.apache.maven.scm.command.diff.AbstractDiffCommand;
028import org.apache.maven.scm.command.diff.DiffScmResult;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.integrity.APISession;
031import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
032import org.codehaus.plexus.util.cli.CommandLineException;
033import org.codehaus.plexus.util.cli.CommandLineUtils;
034import org.codehaus.plexus.util.cli.Commandline;
035
036import java.util.ArrayList;
037import java.util.HashMap;
038
039/**
040 * MKS Integrity implementation for Maven's AbstractDiffCommand
041 * <br>Since MKS Integrity doesn't have a notion of arbitrarily differencing
042 * by a revision across the sandbox, this command will difference the
043 * current Sandbox working file against the server version.
044 *
045 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
046 * @version $Id: IntegrityDiffCommand.java 1.4 2011/08/24 10:29:08EDT Cletus D'Souza (dsouza) Exp  $
047 * @since 1.6
048 */
049public class IntegrityDiffCommand
050    extends AbstractDiffCommand
051{
052    /**
053     * Since we can't arbitrarily apply the same start and end revisions to all files in the sandbox,
054     * this command will be adapted to show differences between the local version and the repository
055     */
056    @Override
057    public DiffScmResult executeDiffCommand( ScmProviderRepository repository, ScmFileSet fileSet,
058                                             ScmVersion startRevision, ScmVersion endRevision )
059        throws ScmException
060    {
061        DiffScmResult result;
062        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
063        APISession api = iRepo.getAPISession();
064        getLogger().info( "Showing differences bettween local files in " + fileSet.getBasedir().getAbsolutePath()
065                              + " and server project " + iRepo.getConfigruationPath() );
066
067        // Since the si diff command is not completely API ready, we will use the CLI for this command
068        Commandline shell = new Commandline();
069        shell.setWorkingDirectory( fileSet.getBasedir() );
070        shell.setExecutable( "si" );
071        shell.createArg().setValue( "diff" );
072        shell.createArg().setValue( "--hostname=" + api.getHostName() );
073        shell.createArg().setValue( "--port=" + api.getPort() );
074        shell.createArg().setValue( "--user=" + api.getUserName() );
075        shell.createArg().setValue( "-R" );
076        shell.createArg().setValue( "--filter=changed:all" );
077        shell.createArg().setValue( "--filter=format:text" );
078        IntegrityDiffConsumer shellConsumer = new IntegrityDiffConsumer( getLogger() );
079
080        try
081        {
082            getLogger().debug( "Executing: " + shell.getCommandline() );
083            int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
084                                                                new CommandLineUtils.StringStreamConsumer() );
085            boolean success = ( exitCode == 128 ? false : true );
086            ScmResult scmResult =
087                new ScmResult( shell.getCommandline().toString(), "", "Exit Code: " + exitCode, success );
088            // Since we can't really parse the differences output, we'll just have to go by the command output
089            // Returning a DiffScmResult(List changedFiles, Map differences, String patch, ScmResult result) to avoid an NPE
090            // in org.codehaus.plexus.util.FileUtils.fileWrite(FileUtils.java:426)
091            return new DiffScmResult( new ArrayList<ScmFile>(), new HashMap<String, CharSequence>(), "", scmResult );
092
093        }
094        catch ( CommandLineException cle )
095        {
096            getLogger().error( "Command Line Exception: " + cle.getMessage() );
097            result = new DiffScmResult( shell.getCommandline().toString(), cle.getMessage(), "", false );
098        }
099
100        return result;
101    }
102
103}