View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.diff;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmResult;
26  import org.apache.maven.scm.ScmVersion;
27  import org.apache.maven.scm.command.diff.AbstractDiffCommand;
28  import org.apache.maven.scm.command.diff.DiffScmResult;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.integrity.APISession;
31  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
32  import org.codehaus.plexus.util.cli.CommandLineException;
33  import org.codehaus.plexus.util.cli.CommandLineUtils;
34  import org.codehaus.plexus.util.cli.Commandline;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  
39  /**
40   * MKS Integrity implementation for Maven's AbstractDiffCommand
41   * <br>Since MKS Integrity doesn't have a notion of arbitrarily differencing
42   * by a revision across the sandbox, this command will difference the
43   * current Sandbox working file against the server version.
44   *
45   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
46   * @since 1.6
47   */
48  public class IntegrityDiffCommand
49      extends AbstractDiffCommand
50  {
51      /**
52       * Since we can't arbitrarily apply the same start and end revisions to all files in the sandbox,
53       * this command will be adapted to show differences between the local version and the repository
54       */
55      @Override
56      public DiffScmResult executeDiffCommand( ScmProviderRepository repository, ScmFileSet fileSet,
57                                               ScmVersion startRevision, ScmVersion endRevision )
58          throws ScmException
59      {
60          DiffScmResult result;
61          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
62          APISession api = iRepo.getAPISession();
63          getLogger().info( "Showing differences bettween local files in " + fileSet.getBasedir().getAbsolutePath()
64                                + " and server project " + iRepo.getConfigruationPath() );
65  
66          // Since the si diff command is not completely API ready, we will use the CLI for this command
67          Commandline shell = new Commandline();
68          shell.setWorkingDirectory( fileSet.getBasedir() );
69          shell.setExecutable( "si" );
70          shell.createArg().setValue( "diff" );
71          shell.createArg().setValue( "--hostname=" + api.getHostName() );
72          shell.createArg().setValue( "--port=" + api.getPort() );
73          shell.createArg().setValue( "--user=" + api.getUserName() );
74          shell.createArg().setValue( "-R" );
75          shell.createArg().setValue( "--filter=changed:all" );
76          shell.createArg().setValue( "--filter=format:text" );
77          IntegrityDiffConsumer shellConsumer = new IntegrityDiffConsumer( getLogger() );
78          String commandLine = CommandLineUtils.toString( shell.getCommandline() );
79  
80          try
81          {
82              getLogger().debug( "Executing: " + commandLine );
83              int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
84                                                                  new CommandLineUtils.StringStreamConsumer() );
85              boolean success = ( exitCode == 128 ? false : true );
86              ScmResult scmResult = new ScmResult( commandLine, "", "Exit Code: " + exitCode, success );
87              // Since we can't really parse the differences output, we'll just have to go by the command output
88              // Returning a DiffScmResult(List changedFiles, Map differences, String patch, ScmResult result) to avoid
89              // a NPE in org.codehaus.plexus.util.FileUtils.fileWrite(FileUtils.java:426)
90              return new DiffScmResult( new ArrayList<ScmFile>(), new HashMap<String, CharSequence>(), "", scmResult );
91  
92          }
93          catch ( CommandLineException cle )
94          {
95              getLogger().error( "Command Line Exception: " + cle.getMessage() );
96              result = new DiffScmResult( commandLine, cle.getMessage(), "", false );
97          }
98  
99          return result;
100     }
101 
102 }