001package org.apache.maven.scm.provider.svn.svnexe.command.export;
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 java.io.File;
023
024import org.apache.maven.scm.ScmBranch;
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmRevision;
028import org.apache.maven.scm.ScmTag;
029import org.apache.maven.scm.ScmVersion;
030import org.apache.maven.scm.command.export.AbstractExportCommand;
031import org.apache.maven.scm.command.export.ExportScmResult;
032import org.apache.maven.scm.command.export.ExportScmResultWithRevision;
033import org.apache.maven.scm.provider.ScmProviderRepository;
034import org.apache.maven.scm.provider.svn.SvnCommandUtils;
035import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
036import org.apache.maven.scm.provider.svn.command.SvnCommand;
037import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
038import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
039import org.apache.maven.scm.provider.svn.svnexe.command.update.SvnUpdateConsumer;
040import org.codehaus.plexus.util.StringUtils;
041import org.codehaus.plexus.util.cli.CommandLineException;
042import org.codehaus.plexus.util.cli.CommandLineUtils;
043import org.codehaus.plexus.util.cli.Commandline;
044
045/**
046 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
047 *
048 */
049public class SvnExeExportCommand
050    extends AbstractExportCommand
051    implements SvnCommand
052{
053    /** {@inheritDoc} */
054    protected ExportScmResult executeExportCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version,
055                                                    String outputDirectory )
056        throws ScmException
057    {
058
059        if ( outputDirectory == null )
060        {
061            outputDirectory = fileSet.getBasedir().getAbsolutePath();
062        }
063
064        SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
065
066        String url = repository.getUrl();
067
068        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
069        {
070            if ( version instanceof ScmTag )
071            {
072                url = SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version );
073            }
074            else if ( version instanceof ScmBranch )
075            {
076                url = SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version );
077            }
078        }
079
080        url = SvnCommandUtils.fixUrl( url, repository.getUser() );
081
082        Commandline cl =
083            createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), version, url, outputDirectory );
084
085        SvnUpdateConsumer consumer = new SvnUpdateConsumer( getLogger(), fileSet.getBasedir() );
086
087        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
088
089        if ( getLogger().isInfoEnabled() )
090        {
091            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
092            if ( cl.getWorkingDirectory() != null )
093            {
094                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
095            }
096        }
097
098        int exitCode;
099
100        try
101        {
102            exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
103        }
104        catch ( CommandLineException ex )
105        {
106            throw new ScmException( "Error while executing command.", ex );
107        }
108
109        if ( exitCode != 0 )
110        {
111            return new ExportScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
112        }
113
114        return new ExportScmResultWithRevision( cl.toString(), consumer.getUpdatedFiles(),
115                                                String.valueOf( consumer.getRevision() ) );
116    }
117
118    // ----------------------------------------------------------------------
119    //
120    // ----------------------------------------------------------------------
121
122    public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, ScmVersion version, String url, String outputSirectory )
123    {
124        if ( version != null && StringUtils.isEmpty( version.getName() ) )
125        {
126            version = null;
127        }
128
129        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
130
131        cl.createArg().setValue( "export" );
132
133        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
134        {
135            if ( version instanceof ScmRevision )
136            {
137                cl.createArg().setValue( "-r" );
138
139                cl.createArg().setValue( version.getName() );
140            }
141        }
142        
143        //support exporting to an existing directory
144        cl.createArg().setValue( "--force" );
145
146        cl.createArg().setValue( url );
147
148        if ( StringUtils.isNotEmpty( outputSirectory ) )
149        {
150            cl.createArg().setValue( outputSirectory );
151        }
152
153        return cl;
154    }
155}