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.export;
20  
21  import java.io.File;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.maven.scm.ScmBranch;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmRevision;
28  import org.apache.maven.scm.ScmTag;
29  import org.apache.maven.scm.ScmVersion;
30  import org.apache.maven.scm.command.export.AbstractExportCommand;
31  import org.apache.maven.scm.command.export.ExportScmResult;
32  import org.apache.maven.scm.command.export.ExportScmResultWithRevision;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.svn.SvnCommandUtils;
35  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
36  import org.apache.maven.scm.provider.svn.command.SvnCommand;
37  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
38  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
39  import org.apache.maven.scm.provider.svn.svnexe.command.update.SvnUpdateConsumer;
40  import org.codehaus.plexus.util.Os;
41  import org.codehaus.plexus.util.cli.CommandLineException;
42  import org.codehaus.plexus.util.cli.CommandLineUtils;
43  import org.codehaus.plexus.util.cli.Commandline;
44  
45  /**
46   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
47   *
48   */
49  public class SvnExeExportCommand extends AbstractExportCommand implements SvnCommand {
50      /** {@inheritDoc} */
51      protected ExportScmResult executeExportCommand(
52              ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, String outputDirectory)
53              throws ScmException {
54  
55          if (outputDirectory == null) {
56              outputDirectory = fileSet.getBasedir().getAbsolutePath();
57          }
58  
59          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
60  
61          String url = repository.getUrl();
62  
63          if (version != null && StringUtils.isNotEmpty(version.getName())) {
64              if (version instanceof ScmTag) {
65                  url = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
66              } else if (version instanceof ScmBranch) {
67                  url = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
68              }
69          }
70  
71          url = SvnCommandUtils.fixUrl(url, repository.getUser());
72  
73          Commandline cl =
74                  createCommandLine((SvnScmProviderRepository) repo, fileSet.getBasedir(), version, url, outputDirectory);
75  
76          SvnUpdateConsumer consumer = new SvnUpdateConsumer(fileSet.getBasedir());
77  
78          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
79  
80          if (logger.isInfoEnabled()) {
81              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
82  
83              if (cl.getWorkingDirectory() != null && Os.isFamily(Os.FAMILY_WINDOWS)) {
84                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
85              }
86          }
87  
88          int exitCode;
89  
90          try {
91              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
92          } catch (CommandLineException ex) {
93              throw new ScmException("Error while executing command.", ex);
94          }
95  
96          if (exitCode != 0) {
97              return new ExportScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
98          }
99  
100         return new ExportScmResultWithRevision(
101                 cl.toString(), consumer.getUpdatedFiles(), String.valueOf(consumer.getRevision()));
102     }
103 
104     // ----------------------------------------------------------------------
105     //
106     // ----------------------------------------------------------------------
107 
108     public static Commandline createCommandLine(
109             SvnScmProviderRepository repository,
110             File workingDirectory,
111             ScmVersion version,
112             String url,
113             String outputSirectory) {
114         if (version != null && StringUtils.isEmpty(version.getName())) {
115             version = null;
116         }
117 
118         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository);
119 
120         cl.createArg().setValue("export");
121 
122         if (version != null && StringUtils.isNotEmpty(version.getName())) {
123             if (version instanceof ScmRevision) {
124                 cl.createArg().setValue("-r");
125 
126                 cl.createArg().setValue(version.getName());
127             }
128         }
129 
130         // support exporting to an existing directory
131         cl.createArg().setValue("--force");
132 
133         cl.createArg().setValue(url + "@");
134 
135         if (outputSirectory != null && !outputSirectory.isEmpty()) {
136             cl.createArg().setValue(outputSirectory + "@");
137         }
138 
139         return cl;
140     }
141 }