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   * @author Olivier Lamy
42   *
43   */
44  public class SvnDiffCommand
45      extends AbstractDiffCommand
46      implements SvnCommand
47  {
48      /** {@inheritDoc} */
49      protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion,
50                                                  ScmVersion endVersion )
51          throws ScmException
52      {
53          Commandline cl =
54              createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), startVersion, endVersion );
55  
56          SvnDiffConsumer consumer = new SvnDiffConsumer( getLogger(), fileSet.getBasedir() );
57  
58          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
59  
60          if ( getLogger().isInfoEnabled() )
61          {
62              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
63              getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
64          }
65  
66          int exitCode;
67  
68          try
69          {
70              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
71          }
72          catch ( CommandLineException ex )
73          {
74              throw new ScmException( "Error while executing command.", ex );
75          }
76  
77          if ( exitCode != 0 )
78          {
79              return new DiffScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
80          }
81  
82          return new DiffScmResult( cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
83                                    consumer.getPatch() );
84      }
85  
86      // ----------------------------------------------------------------------
87      //
88      // ----------------------------------------------------------------------
89  
90      public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
91                                                   ScmVersion startVersion, ScmVersion endVersion )
92      {
93          Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
94  
95          cl.createArg().setValue( "diff" );
96  
97          if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
98          {
99              cl.createArg().setValue( "-r" );
100 
101             if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) )
102             {
103                 cl.createArg().setValue( startVersion.getName() + ":" + endVersion.getName() );
104             }
105             else
106             {
107                 cl.createArg().setValue( startVersion.getName() );
108             }
109         }
110 
111         return cl;
112     }
113 }