001package 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
022import org.apache.maven.scm.ScmBranch;
023import org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.ScmRevision;
026import org.apache.maven.scm.ScmTag;
027import org.apache.maven.scm.ScmVersion;
028import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
029import org.apache.maven.scm.command.checkout.CheckOutScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.svn.SvnCommandUtils;
032import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
033import org.apache.maven.scm.provider.svn.command.SvnCommand;
034import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
035import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
036import org.codehaus.plexus.util.Os;
037import org.codehaus.plexus.util.StringUtils;
038import org.codehaus.plexus.util.cli.CommandLineException;
039import org.codehaus.plexus.util.cli.CommandLineUtils;
040import org.codehaus.plexus.util.cli.Commandline;
041
042import java.io.File;
043
044/**
045 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
046 * @author Olivier Lamy
047 *
048 */
049public class SvnCheckOutCommand
050    extends AbstractCheckOutCommand
051    implements SvnCommand
052{
053    /**
054     * {@inheritDoc}
055     */
056    protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
057                                                        ScmVersion version, boolean recursive )
058        throws ScmException
059    {
060        SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
061
062        String url = repository.getUrl();
063
064        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
065        {
066            if ( version instanceof ScmTag )
067            {
068                url = SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version );
069            }
070            else if ( version instanceof ScmBranch )
071            {
072                url = SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version );
073            }
074        }
075
076        url = SvnCommandUtils.fixUrl( url, repository.getUser() );
077
078        Commandline cl = createCommandLine( repository, fileSet.getBasedir(), version, url, recursive );
079
080        SvnCheckOutConsumer consumer = new SvnCheckOutConsumer( getLogger(), fileSet.getBasedir() );
081
082        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
083
084        int exitCode;
085
086        if ( getLogger().isInfoEnabled() )
087        {
088            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
089
090            if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
091            {
092                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
093            }
094        }
095
096        try
097        {
098            exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
099        }
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}