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   * @version $Id: IntegrityDiffCommand.java 1.4 2011/08/24 10:29:08EDT Cletus D'Souza (dsouza) Exp  $
47   * @since 1.6
48   */
49  public class IntegrityDiffCommand
50      extends AbstractDiffCommand
51  {
52      /**
53       * Since we can't arbitrarily apply the same start and end revisions to all files in the sandbox,
54       * this command will be adapted to show differences between the local version and the repository
55       */
56      @Override
57      public DiffScmResult executeDiffCommand( ScmProviderRepository repository, ScmFileSet fileSet,
58                                               ScmVersion startRevision, ScmVersion endRevision )
59          throws ScmException
60      {
61          DiffScmResult result;
62          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
63          APISession api = iRepo.getAPISession();
64          getLogger().info( "Showing differences bettween local files in " + fileSet.getBasedir().getAbsolutePath()
65                                + " and server project " + iRepo.getConfigruationPath() );
66  
67          // Since the si diff command is not completely API ready, we will use the CLI for this command
68          Commandline shell = new Commandline();
69          shell.setWorkingDirectory( fileSet.getBasedir() );
70          shell.setExecutable( "si" );
71          shell.createArg().setValue( "diff" );
72          shell.createArg().setValue( "--hostname=" + api.getHostName() );
73          shell.createArg().setValue( "--port=" + api.getPort() );
74          shell.createArg().setValue( "--user=" + api.getUserName() );
75          shell.createArg().setValue( "-R" );
76          shell.createArg().setValue( "--filter=changed:all" );
77          shell.createArg().setValue( "--filter=format:text" );
78          IntegrityDiffConsumer shellConsumer = new IntegrityDiffConsumer( getLogger() );
79  
80          try
81          {
82              getLogger().debug( "Executing: " + shell.getCommandline() );
83              int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
84                                                                  new CommandLineUtils.StringStreamConsumer() );
85              boolean success = ( exitCode == 128 ? false : true );
86              ScmResult scmResult =
87                  new ScmResult( shell.getCommandline().toString(), "", "Exit Code: " + exitCode, success );
88              // Since we can't really parse the differences output, we'll just have to go by the command output
89              // Returning a DiffScmResult(List changedFiles, Map differences, String patch, ScmResult result) to avoid an NPE
90              // in org.codehaus.plexus.util.FileUtils.fileWrite(FileUtils.java:426)
91              return new DiffScmResult( new ArrayList<ScmFile>(), new HashMap<String, CharSequence>(), "", scmResult );
92  
93          }
94          catch ( CommandLineException cle )
95          {
96              getLogger().error( "Command Line Exception: " + cle.getMessage() );
97              result = new DiffScmResult( shell.getCommandline().toString(), cle.getMessage(), "", false );
98          }
99  
100         return result;
101     }
102 
103 }