001    package org.apache.maven.scm.provider.svn.svnexe.command.checkout;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.scm.ScmBranch;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.ScmRevision;
026    import org.apache.maven.scm.ScmTag;
027    import org.apache.maven.scm.ScmVersion;
028    import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
029    import org.apache.maven.scm.command.checkout.CheckOutScmResult;
030    import org.apache.maven.scm.provider.ScmProviderRepository;
031    import org.apache.maven.scm.provider.svn.SvnCommandUtils;
032    import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
033    import org.apache.maven.scm.provider.svn.command.SvnCommand;
034    import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
035    import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
036    import org.codehaus.plexus.util.StringUtils;
037    import org.codehaus.plexus.util.cli.CommandLineException;
038    import org.codehaus.plexus.util.cli.CommandLineUtils;
039    import org.codehaus.plexus.util.cli.Commandline;
040    
041    import java.io.File;
042    
043    /**
044     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
045     * @author Olivier Lamy
046     * @version $Id: SvnCheckOutCommand.java 1350009 2012-06-13 20:49:42Z olamy $
047     */
048    public class SvnCheckOutCommand
049        extends AbstractCheckOutCommand
050        implements SvnCommand
051    {
052        /**
053         * {@inheritDoc}
054         */
055        protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
056                                                            ScmVersion version, boolean recursive )
057            throws ScmException
058        {
059            SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
060    
061            String url = repository.getUrl();
062    
063            if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
064            {
065                if ( version instanceof ScmTag )
066                {
067                    url = SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version );
068                }
069                else if ( version instanceof ScmBranch )
070                {
071                    url = SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version );
072                }
073            }
074    
075            url = SvnCommandUtils.fixUrl( url, repository.getUser() );
076    
077            Commandline cl = createCommandLine( repository, fileSet.getBasedir(), version, url, recursive );
078    
079            SvnCheckOutConsumer consumer = new SvnCheckOutConsumer( getLogger(), fileSet.getBasedir() );
080    
081            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
082    
083            int exitCode;
084    
085            if ( getLogger().isInfoEnabled() )
086            {
087                getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
088                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
089            }
090    
091            try
092            {
093                exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
094            }
095            catch ( CommandLineException ex )
096            {
097                throw new ScmException( "Error while executing command.", ex );
098            }
099    
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    }