View Javadoc

1   package org.apache.maven.scm.provider.svn.svnexe.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.svn.command.SvnCommand;
29  import org.apache.maven.scm.provider.svn.command.diff.SvnDiffConsumer;
30  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
31  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.codehaus.plexus.util.cli.CommandLineException;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  import java.io.File;
38  
39  /**
40   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
41   * @version $Id: SvnDiffCommand.java 524909 2007-04-02 20:02:44Z evenisse $
42   */
43  public class SvnDiffCommand
44      extends AbstractDiffCommand
45      implements SvnCommand
46  {
47      protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion,
48                                                  ScmVersion endVersion )
49          throws ScmException
50      {
51          Commandline cl =
52              createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), startVersion, endVersion );
53  
54          SvnDiffConsumer consumer = new SvnDiffConsumer( getLogger(), fileSet.getBasedir() );
55  
56          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
57  
58          getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
59          getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
60  
61          int exitCode;
62  
63          try
64          {
65              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
66          }
67          catch ( CommandLineException ex )
68          {
69              throw new ScmException( "Error while executing command.", ex );
70          }
71  
72          if ( exitCode != 0 )
73          {
74              return new DiffScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
75          }
76  
77          return new DiffScmResult( cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
78                                    consumer.getPatch() );
79      }
80  
81      // ----------------------------------------------------------------------
82      //
83      // ----------------------------------------------------------------------
84  
85      public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
86                                                   ScmVersion startVersion, ScmVersion endVersion )
87      {
88          Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
89  
90          cl.createArgument().setValue( "diff" );
91  
92          if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
93          {
94              cl.createArgument().setValue( "-r" );
95  
96              if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) )
97              {
98                  cl.createArgument().setValue( startVersion.getName() + ":" + endVersion.getName() );
99              }
100             else
101             {
102                 cl.createArgument().setValue( startVersion.getName() );
103             }
104         }
105 
106         return cl;
107     }
108 }