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