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