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 * @since 1.6
047 */
048public class IntegrityDiffCommand
049    extends AbstractDiffCommand
050{
051    /**
052     * Since we can't arbitrarily apply the same start and end revisions to all files in the sandbox,
053     * this command will be adapted to show differences between the local version and the repository
054     */
055    @Override
056    public DiffScmResult executeDiffCommand( ScmProviderRepository repository, ScmFileSet fileSet,
057                                             ScmVersion startRevision, ScmVersion endRevision )
058        throws ScmException
059    {
060        DiffScmResult result;
061        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
062        APISession api = iRepo.getAPISession();
063        getLogger().info( "Showing differences bettween local files in " + fileSet.getBasedir().getAbsolutePath()
064                              + " and server project " + iRepo.getConfigruationPath() );
065
066        // Since the si diff command is not completely API ready, we will use the CLI for this command
067        Commandline shell = new Commandline();
068        shell.setWorkingDirectory( fileSet.getBasedir() );
069        shell.setExecutable( "si" );
070        shell.createArg().setValue( "diff" );
071        shell.createArg().setValue( "--hostname=" + api.getHostName() );
072        shell.createArg().setValue( "--port=" + api.getPort() );
073        shell.createArg().setValue( "--user=" + api.getUserName() );
074        shell.createArg().setValue( "-R" );
075        shell.createArg().setValue( "--filter=changed:all" );
076        shell.createArg().setValue( "--filter=format:text" );
077        IntegrityDiffConsumer shellConsumer = new IntegrityDiffConsumer( getLogger() );
078        String commandLine = CommandLineUtils.toString( shell.getCommandline() );
079
080        try
081        {
082            getLogger().debug( "Executing: " + commandLine );
083            int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
084                                                                new CommandLineUtils.StringStreamConsumer() );
085            boolean success = ( exitCode == 128 ? false : true );
086            ScmResult scmResult = new ScmResult( commandLine, "", "Exit Code: " + exitCode, success );
087            // Since we can't really parse the differences output, we'll just have to go by the command output
088            // Returning a DiffScmResult(List changedFiles, Map differences, String patch, ScmResult result) to avoid
089            // a NPE in org.codehaus.plexus.util.FileUtils.fileWrite(FileUtils.java:426)
090            return new DiffScmResult( new ArrayList<ScmFile>(), new HashMap<String, CharSequence>(), "", scmResult );
091
092        }
093        catch ( CommandLineException cle )
094        {
095            getLogger().error( "Command Line Exception: " + cle.getMessage() );
096            result = new DiffScmResult( commandLine, cle.getMessage(), "", false );
097        }
098
099        return result;
100    }
101
102}