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.checkout;
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.checkout.AbstractCheckOutCommand;
031import org.apache.maven.scm.command.checkout.CheckOutScmResult;
032import org.apache.maven.scm.provider.ScmProviderRepository;
033import org.apache.maven.scm.provider.svn.SvnCommandUtils;
034import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
035import org.apache.maven.scm.provider.svn.command.SvnCommand;
036import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
037import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
038import org.codehaus.plexus.util.Os;
039import org.codehaus.plexus.util.cli.CommandLineException;
040import org.codehaus.plexus.util.cli.CommandLineUtils;
041import org.codehaus.plexus.util.cli.Commandline;
042
043/**
044 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
045 * @author Olivier Lamy
046 *
047 */
048public class SvnCheckOutCommand extends AbstractCheckOutCommand implements SvnCommand {
049    /**
050     * {@inheritDoc}
051     */
052    protected CheckOutScmResult executeCheckOutCommand(
053            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
054            throws ScmException {
055        SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
056
057        String url = repository.getUrl();
058
059        if (version != null && StringUtils.isNotEmpty(version.getName())) {
060            if (version instanceof ScmTag) {
061                url = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
062            } else if (version instanceof ScmBranch) {
063                url = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
064            }
065        }
066
067        url = SvnCommandUtils.fixUrl(url, repository.getUser());
068
069        Commandline cl = createCommandLine(repository, fileSet.getBasedir(), version, url, recursive);
070
071        SvnCheckOutConsumer consumer = new SvnCheckOutConsumer(fileSet.getBasedir());
072
073        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
074
075        int exitCode;
076
077        if (logger.isInfoEnabled()) {
078            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
079
080            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
081                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
082            }
083        }
084
085        try {
086            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
087        } catch (CommandLineException ex) {
088            throw new ScmException("Error while executing command.", ex);
089        }
090
091        if (exitCode != 0) {
092            return new CheckOutScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
093        }
094
095        return new CheckOutScmResult(
096                cl.toString(), Integer.toString(consumer.getRevision()), consumer.getCheckedOutFiles());
097    }
098
099    // ----------------------------------------------------------------------
100    //
101    // ----------------------------------------------------------------------
102
103    /**
104     * Create SVN check out command line in a recursive way.
105     *
106     * @param repository       not null
107     * @param workingDirectory not null
108     * @param version          not null
109     * @param url              not null
110     * @return the SVN command line for the SVN check out.
111     * @see #createCommandLine(SvnScmProviderRepository, File, ScmVersion, String, boolean)
112     */
113    public static Commandline createCommandLine(
114            SvnScmProviderRepository repository, File workingDirectory, ScmVersion version, String url) {
115        return createCommandLine(repository, workingDirectory, version, url, true);
116    }
117
118    /**
119     * Create SVN check out command line.
120     *
121     * @param repository       not null
122     * @param workingDirectory not null
123     * @param version          not null
124     * @param url              not null
125     * @param recursive        <code>true</code> if recursive check out is wanted, <code>false</code> otherwise.
126     * @return the SVN command line for the SVN check out.
127     * @since 1.1.1
128     */
129    public static Commandline createCommandLine(
130            SvnScmProviderRepository repository,
131            File workingDirectory,
132            ScmVersion version,
133            String url,
134            boolean recursive) {
135        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory.getParentFile(), repository);
136
137        cl.createArg().setValue("checkout");
138
139        // add non recursive option
140        if (!recursive) {
141            cl.createArg().setValue("-N");
142        }
143
144        if (version != null && StringUtils.isNotEmpty(version.getName())) {
145            if (version instanceof ScmRevision) {
146                cl.createArg().setValue("-r");
147
148                cl.createArg().setValue(version.getName());
149            }
150        }
151
152        cl.createArg().setValue(url + "@");
153
154        cl.createArg().setValue(workingDirectory.getAbsolutePath());
155
156        return cl;
157    }
158}