View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.checkout;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmBranch;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmRevision;
26  import org.apache.maven.scm.ScmTag;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
29  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.svn.SvnCommandUtils;
32  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
33  import org.apache.maven.scm.provider.svn.command.SvnCommand;
34  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
35  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
36  import org.codehaus.plexus.util.Os;
37  import org.codehaus.plexus.util.StringUtils;
38  import org.codehaus.plexus.util.cli.CommandLineException;
39  import org.codehaus.plexus.util.cli.CommandLineUtils;
40  import org.codehaus.plexus.util.cli.Commandline;
41  
42  import java.io.File;
43  
44  /**
45   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
46   * @author Olivier Lamy
47   *
48   */
49  public class SvnCheckOutCommand
50      extends AbstractCheckOutCommand
51      implements SvnCommand
52  {
53      /**
54       * {@inheritDoc}
55       */
56      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
57                                                         ScmVersion version, boolean recursive, boolean shallow )
58          throws ScmException
59      {
60          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
61  
62          String url = repository.getUrl();
63  
64          if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
65          {
66              if ( version instanceof ScmTag )
67              {
68                  url = SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version );
69              }
70              else if ( version instanceof ScmBranch )
71              {
72                  url = SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version );
73              }
74          }
75  
76          url = SvnCommandUtils.fixUrl( url, repository.getUser() );
77  
78          Commandline cl = createCommandLine( repository, fileSet.getBasedir(), version, url, recursive );
79  
80          SvnCheckOutConsumer consumer = new SvnCheckOutConsumer( getLogger(), fileSet.getBasedir() );
81  
82          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
83  
84          int exitCode;
85  
86          if ( getLogger().isInfoEnabled() )
87          {
88              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
89  
90              if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
91              {
92                  getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
93              }
94          }
95  
96          try
97          {
98              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
99          }
100         catch ( CommandLineException ex )
101         {
102             throw new ScmException( "Error while executing command.", ex );
103         }
104 
105         if ( exitCode != 0 )
106         {
107             return new CheckOutScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
108         }
109 
110         return new CheckOutScmResult( cl.toString(), Integer.toString( consumer.getRevision() ),
111                                       consumer.getCheckedOutFiles() );
112     }
113 
114     // ----------------------------------------------------------------------
115     //
116     // ----------------------------------------------------------------------
117 
118     /**
119      * Create SVN check out command line in a recursive way.
120      *
121      * @param repository       not null
122      * @param workingDirectory not null
123      * @param version          not null
124      * @param url              not null
125      * @return the SVN command line for the SVN check out.
126      * @see #createCommandLine(SvnScmProviderRepository, File, ScmVersion, String, boolean)
127      */
128     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
129                                                  ScmVersion version, String url )
130     {
131         return createCommandLine( repository, workingDirectory, version, url, true );
132     }
133 
134     /**
135      * Create SVN check out command line.
136      *
137      * @param repository       not null
138      * @param workingDirectory not null
139      * @param version          not null
140      * @param url              not null
141      * @param recursive        <code>true</code> if recursive check out is wanted, <code>false</code> otherwise.
142      * @return the SVN command line for the SVN check out.
143      * @since 1.1.1
144      */
145     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
146                                                  ScmVersion version, String url, boolean recursive )
147     {
148         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory.getParentFile(), repository );
149 
150         cl.createArg().setValue( "checkout" );
151 
152         // add non recursive option
153         if ( !recursive )
154         {
155             cl.createArg().setValue( "-N" );
156         }
157 
158         if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
159         {
160             if ( version instanceof ScmRevision )
161             {
162                 cl.createArg().setValue( "-r" );
163 
164                 cl.createArg().setValue( version.getName() );
165             }
166         }
167 
168         cl.createArg().setValue( url + "@" );
169 
170         cl.createArg().setValue( workingDirectory.getAbsolutePath() );
171 
172         return cl;
173     }
174 }