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