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