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  public class SvnCheckOutCommand extends AbstractCheckOutCommand implements SvnCommand {
48      private final boolean interactive;
49  
50      public SvnCheckOutCommand(boolean interactive) {
51          this.interactive = interactive;
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      protected CheckOutScmResult executeCheckOutCommand(
58              ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
59              throws ScmException {
60          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
61  
62          String url = repository.getUrl();
63  
64          if (version != null && StringUtils.isNotEmpty(version.getName())) {
65              if (version instanceof ScmTag) {
66                  url = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
67              } else if (version instanceof ScmBranch) {
68                  url = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
69              }
70          }
71  
72          url = SvnCommandUtils.fixUrl(url, repository.getUser());
73  
74          Commandline cl = createCommandLine(repository, fileSet.getBasedir(), version, url, recursive);
75  
76          SvnCheckOutConsumer consumer = new SvnCheckOutConsumer(fileSet.getBasedir());
77  
78          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
79  
80          int exitCode;
81  
82          if (logger.isInfoEnabled()) {
83              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
84  
85              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
86                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
87              }
88          }
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 CheckOutScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
98          }
99  
100         return new CheckOutScmResult(
101                 cl.toString(), Integer.toString(consumer.getRevision()), consumer.getCheckedOutFiles());
102     }
103 
104     // ----------------------------------------------------------------------
105     //
106     // ----------------------------------------------------------------------
107 
108     /**
109      * Create SVN check out command line in a recursive way.
110      *
111      * @param repository       not null
112      * @param workingDirectory not null
113      * @param version          not null
114      * @param url              not null
115      * @return the SVN command line for the SVN check out
116      * @see #createCommandLine(SvnScmProviderRepository, File, ScmVersion, String, boolean)
117      */
118     public Commandline createCommandLine(
119             SvnScmProviderRepository repository, File workingDirectory, ScmVersion version, String url) {
120         return createCommandLine(repository, workingDirectory, version, url, true);
121     }
122 
123     /**
124      * Create SVN check out command line.
125      *
126      * @param repository       not null
127      * @param workingDirectory not null
128      * @param version          not null
129      * @param url              not null
130      * @param recursive        <code>true</code> if recursive check out is wanted, <code>false</code> otherwise
131      * @return the SVN command line for the SVN check out
132      * @since 1.1.1
133      */
134     public Commandline createCommandLine(
135             SvnScmProviderRepository repository,
136             File workingDirectory,
137             ScmVersion version,
138             String url,
139             boolean recursive) {
140         Commandline cl =
141                 SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory.getParentFile(), repository, interactive);
142 
143         cl.createArg().setValue("checkout");
144 
145         // add non recursive option
146         if (!recursive) {
147             cl.createArg().setValue("-N");
148         }
149 
150         if (version != null && StringUtils.isNotEmpty(version.getName())) {
151             if (version instanceof ScmRevision) {
152                 cl.createArg().setValue("-r");
153 
154                 cl.createArg().setValue(version.getName());
155             }
156         }
157 
158         cl.createArg().setValue(url + "@");
159 
160         cl.createArg().setValue(workingDirectory.getAbsolutePath());
161 
162         return cl;
163     }
164 }