001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn.svnexe.command.export;
020
021import java.io.File;
022
023import org.apache.commons.lang3.StringUtils;
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.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 extends AbstractExportCommand implements SvnCommand {
050    /** {@inheritDoc} */
051    protected ExportScmResult executeExportCommand(
052            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, String outputDirectory)
053            throws ScmException {
054
055        if (outputDirectory == null) {
056            outputDirectory = fileSet.getBasedir().getAbsolutePath();
057        }
058
059        SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
060
061        String url = repository.getUrl();
062
063        if (version != null && StringUtils.isNotEmpty(version.getName())) {
064            if (version instanceof ScmTag) {
065                url = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
066            } else if (version instanceof ScmBranch) {
067                url = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
068            }
069        }
070
071        url = SvnCommandUtils.fixUrl(url, repository.getUser());
072
073        Commandline cl =
074                createCommandLine((SvnScmProviderRepository) repo, fileSet.getBasedir(), version, url, outputDirectory);
075
076        SvnUpdateConsumer consumer = new SvnUpdateConsumer(fileSet.getBasedir());
077
078        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
079
080        if (logger.isInfoEnabled()) {
081            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
082
083            if (cl.getWorkingDirectory() != null && Os.isFamily(Os.FAMILY_WINDOWS)) {
084                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
085            }
086        }
087
088        int exitCode;
089
090        try {
091            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
092        } catch (CommandLineException ex) {
093            throw new ScmException("Error while executing command.", ex);
094        }
095
096        if (exitCode != 0) {
097            return new ExportScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
098        }
099
100        return new ExportScmResultWithRevision(
101                cl.toString(), consumer.getUpdatedFiles(), String.valueOf(consumer.getRevision()));
102    }
103
104    // ----------------------------------------------------------------------
105    //
106    // ----------------------------------------------------------------------
107
108    public static Commandline createCommandLine(
109            SvnScmProviderRepository repository,
110            File workingDirectory,
111            ScmVersion version,
112            String url,
113            String outputSirectory) {
114        if (version != null && StringUtils.isEmpty(version.getName())) {
115            version = null;
116        }
117
118        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository);
119
120        cl.createArg().setValue("export");
121
122        if (version != null && StringUtils.isNotEmpty(version.getName())) {
123            if (version instanceof ScmRevision) {
124                cl.createArg().setValue("-r");
125
126                cl.createArg().setValue(version.getName());
127            }
128        }
129
130        // support exporting to an existing directory
131        cl.createArg().setValue("--force");
132
133        cl.createArg().setValue(url + "@");
134
135        if (outputSirectory != null && !outputSirectory.isEmpty()) {
136            cl.createArg().setValue(outputSirectory + "@");
137        }
138
139        return cl;
140    }
141}