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.checkout;
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.checkout.AbstractCheckOutCommand;
31  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
32  import org.apache.maven.scm.provider.ScmProviderRepository;
33  import org.apache.maven.scm.provider.svn.SvnCommandUtils;
34  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
35  import org.apache.maven.scm.provider.svn.command.SvnCommand;
36  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
37  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
38  import org.codehaus.plexus.util.Os;
39  import org.codehaus.plexus.util.cli.CommandLineException;
40  import org.codehaus.plexus.util.cli.CommandLineUtils;
41  import org.codehaus.plexus.util.cli.Commandline;
42  
43  /**
44   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
45   * @author Olivier Lamy
46   *
47   */
48  public class SvnCheckOutCommand extends AbstractCheckOutCommand implements SvnCommand {
49      /**
50       * {@inheritDoc}
51       */
52      protected CheckOutScmResult executeCheckOutCommand(
53              ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
54              throws ScmException {
55          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
56  
57          String url = repository.getUrl();
58  
59          if (version != null && StringUtils.isNotEmpty(version.getName())) {
60              if (version instanceof ScmTag) {
61                  url = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
62              } else if (version instanceof ScmBranch) {
63                  url = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
64              }
65          }
66  
67          url = SvnCommandUtils.fixUrl(url, repository.getUser());
68  
69          Commandline cl = createCommandLine(repository, fileSet.getBasedir(), version, url, recursive);
70  
71          SvnCheckOutConsumer consumer = new SvnCheckOutConsumer(fileSet.getBasedir());
72  
73          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
74  
75          int exitCode;
76  
77          if (logger.isInfoEnabled()) {
78              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
79  
80              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
81                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
82              }
83          }
84  
85          try {
86              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
87          } catch (CommandLineException ex) {
88              throw new ScmException("Error while executing command.", ex);
89          }
90  
91          if (exitCode != 0) {
92              return new CheckOutScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
93          }
94  
95          return new CheckOutScmResult(
96                  cl.toString(), Integer.toString(consumer.getRevision()), consumer.getCheckedOutFiles());
97      }
98  
99      // ----------------------------------------------------------------------
100     //
101     // ----------------------------------------------------------------------
102 
103     /**
104      * Create SVN check out command line in a recursive way.
105      *
106      * @param repository       not null
107      * @param workingDirectory not null
108      * @param version          not null
109      * @param url              not null
110      * @return the SVN command line for the SVN check out.
111      * @see #createCommandLine(SvnScmProviderRepository, File, ScmVersion, String, boolean)
112      */
113     public static Commandline createCommandLine(
114             SvnScmProviderRepository repository, File workingDirectory, ScmVersion version, String url) {
115         return createCommandLine(repository, workingDirectory, version, url, true);
116     }
117 
118     /**
119      * Create SVN check out command line.
120      *
121      * @param repository       not null
122      * @param workingDirectory not null
123      * @param version          not null
124      * @param url              not null
125      * @param recursive        <code>true</code> if recursive check out is wanted, <code>false</code> otherwise.
126      * @return the SVN command line for the SVN check out.
127      * @since 1.1.1
128      */
129     public static Commandline createCommandLine(
130             SvnScmProviderRepository repository,
131             File workingDirectory,
132             ScmVersion version,
133             String url,
134             boolean recursive) {
135         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory.getParentFile(), repository);
136 
137         cl.createArg().setValue("checkout");
138 
139         // add non recursive option
140         if (!recursive) {
141             cl.createArg().setValue("-N");
142         }
143 
144         if (version != null && StringUtils.isNotEmpty(version.getName())) {
145             if (version instanceof ScmRevision) {
146                 cl.createArg().setValue("-r");
147 
148                 cl.createArg().setValue(version.getName());
149             }
150         }
151 
152         cl.createArg().setValue(url + "@");
153 
154         cl.createArg().setValue(workingDirectory.getAbsolutePath());
155 
156         return cl;
157     }
158 }