View Javadoc

1   package org.apache.maven.scm.provider.perforce.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.ScmFileSet;
24  import org.apache.maven.scm.ScmVersion;
25  import org.apache.maven.scm.command.diff.AbstractDiffCommand;
26  import org.apache.maven.scm.command.diff.DiffScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
29  import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
30  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
31  import org.codehaus.plexus.util.StringUtils;
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.io.File;
37  
38  /**
39   * @author Mike Perham
40   *
41   */
42  public class PerforceDiffCommand
43      extends AbstractDiffCommand
44      implements PerforceCommand
45  {
46      /** {@inheritDoc} */
47      protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet files, ScmVersion startRev,
48                                                  ScmVersion endRev )
49          throws ScmException
50      {
51          Commandline cl =
52              createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), startRev, endRev );
53          PerforceDiffConsumer consumer = new PerforceDiffConsumer();
54          if ( getLogger().isInfoEnabled() )
55          {
56              getLogger().info( "Executing: " + PerforceScmProvider.clean( cl.toString() ) );
57          }
58          boolean success = false;
59          try
60          {
61              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
62              int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
63  
64              if ( exitCode != 0 )
65              {
66                  String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
67  
68                  StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
69                  msg.append( '\n' );
70                  msg.append( "Command line was:" + cmdLine );
71  
72                  throw new CommandLineException( msg.toString() );
73              }
74          }
75          catch ( CommandLineException e )
76          {
77              if ( getLogger().isErrorEnabled() )
78              {
79                  getLogger().error( "CommandLineException " + e.getMessage(), e );
80              }
81          }
82  
83          return new DiffScmResult( cl.toString(), success ? "Diff successful" : "Unable to diff", consumer
84              .getOutput(), success );
85      }
86  
87      public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
88                                                   ScmVersion startRev, ScmVersion endRev )
89      {
90          String start = startRev != null && StringUtils.isNotEmpty( startRev.getName() ) ? "@" + startRev.getName() : "";
91          String end = endRev != null && StringUtils.isNotEmpty( endRev.getName() ) ? endRev.getName() : "now";
92  
93          Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
94  
95          command.createArg().setValue( "diff2" );
96          command.createArg().setValue( "-u" );
97          // I'm assuming the "revs" are actually labels
98          command.createArg().setValue( "..." + start );
99          command.createArg().setValue( "...@" + end );
100         return command;
101     }
102 
103 }