View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn.svnexe.command.diff;
20  
21  import java.io.File;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
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.svn.command.SvnCommand;
31  import org.apache.maven.scm.provider.svn.command.diff.SvnDiffConsumer;
32  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
33  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
34  import org.codehaus.plexus.util.Os;
35  import org.codehaus.plexus.util.cli.CommandLineException;
36  import org.codehaus.plexus.util.cli.CommandLineUtils;
37  import org.codehaus.plexus.util.cli.Commandline;
38  
39  /**
40   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
41   * @author Olivier Lamy
42   *
43   */
44  public class SvnDiffCommand extends AbstractDiffCommand implements SvnCommand {
45      /** {@inheritDoc} */
46      protected DiffScmResult executeDiffCommand(
47              ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion)
48              throws ScmException {
49          Commandline cl =
50                  createCommandLine((SvnScmProviderRepository) repo, fileSet.getBasedir(), startVersion, endVersion);
51  
52          SvnDiffConsumer consumer = new SvnDiffConsumer(fileSet.getBasedir());
53  
54          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
55  
56          if (logger.isInfoEnabled()) {
57              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
58  
59              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
60                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
61              }
62          }
63  
64          int exitCode;
65  
66          try {
67              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
68          } catch (CommandLineException ex) {
69              throw new ScmException("Error while executing command.", ex);
70          }
71  
72          if (exitCode != 0) {
73              return new DiffScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
74          }
75  
76          return new DiffScmResult(
77                  cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch());
78      }
79  
80      // ----------------------------------------------------------------------
81      //
82      // ----------------------------------------------------------------------
83  
84      public static Commandline createCommandLine(
85              SvnScmProviderRepository repository,
86              File workingDirectory,
87              ScmVersion startVersion,
88              ScmVersion endVersion) {
89          Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository);
90  
91          cl.createArg().setValue("diff");
92  
93          cl.createArg().setValue("--internal-diff");
94  
95          if (startVersion != null && StringUtils.isNotEmpty(startVersion.getName())) {
96              cl.createArg().setValue("-r");
97  
98              if (endVersion != null && StringUtils.isNotEmpty(endVersion.getName())) {
99                  cl.createArg().setValue(startVersion.getName() + ":" + endVersion.getName());
100             } else {
101                 cl.createArg().setValue(startVersion.getName());
102             }
103         }
104 
105         return cl;
106     }
107 }