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 */
047public class SvnCheckOutCommand extends AbstractCheckOutCommand implements SvnCommand {
048    private final boolean interactive;
049
050    public SvnCheckOutCommand(boolean interactive) {
051        this.interactive = interactive;
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    protected CheckOutScmResult executeCheckOutCommand(
058            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
059            throws ScmException {
060        SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
061
062        String url = repository.getUrl();
063
064        if (version != null && StringUtils.isNotEmpty(version.getName())) {
065            if (version instanceof ScmTag) {
066                url = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
067            } else if (version instanceof ScmBranch) {
068                url = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
069            }
070        }
071
072        url = SvnCommandUtils.fixUrl(url, repository.getUser());
073
074        Commandline cl = createCommandLine(repository, fileSet.getBasedir(), version, url, recursive);
075
076        SvnCheckOutConsumer consumer = new SvnCheckOutConsumer(fileSet.getBasedir());
077
078        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
079
080        int exitCode;
081
082        if (logger.isInfoEnabled()) {
083            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
084
085            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
086                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
087            }
088        }
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 CheckOutScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
098        }
099
100        return new CheckOutScmResult(
101                cl.toString(), Integer.toString(consumer.getRevision()), consumer.getCheckedOutFiles());
102    }
103
104    // ----------------------------------------------------------------------
105    //
106    // ----------------------------------------------------------------------
107
108    /**
109     * Create SVN check out command line in a recursive way.
110     *
111     * @param repository       not null
112     * @param workingDirectory not null
113     * @param version          not null
114     * @param url              not null
115     * @return the SVN command line for the SVN check out
116     * @see #createCommandLine(SvnScmProviderRepository, File, ScmVersion, String, boolean)
117     */
118    public Commandline createCommandLine(
119            SvnScmProviderRepository repository, File workingDirectory, ScmVersion version, String url) {
120        return createCommandLine(repository, workingDirectory, version, url, true);
121    }
122
123    /**
124     * Create SVN check out command line.
125     *
126     * @param repository       not null
127     * @param workingDirectory not null
128     * @param version          not null
129     * @param url              not null
130     * @param recursive        <code>true</code> if recursive check out is wanted, <code>false</code> otherwise
131     * @return the SVN command line for the SVN check out
132     * @since 1.1.1
133     */
134    public Commandline createCommandLine(
135            SvnScmProviderRepository repository,
136            File workingDirectory,
137            ScmVersion version,
138            String url,
139            boolean recursive) {
140        Commandline cl =
141                SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory.getParentFile(), repository, interactive);
142
143        cl.createArg().setValue("checkout");
144
145        // add non recursive option
146        if (!recursive) {
147            cl.createArg().setValue("-N");
148        }
149
150        if (version != null && StringUtils.isNotEmpty(version.getName())) {
151            if (version instanceof ScmRevision) {
152                cl.createArg().setValue("-r");
153
154                cl.createArg().setValue(version.getName());
155            }
156        }
157
158        cl.createArg().setValue(url + "@");
159
160        cl.createArg().setValue(workingDirectory.getAbsolutePath());
161
162        return cl;
163    }
164}