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.update;
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.ScmTag;
028import org.apache.maven.scm.ScmVersion;
029import org.apache.maven.scm.command.changelog.ChangeLogCommand;
030import org.apache.maven.scm.command.update.AbstractUpdateCommand;
031import org.apache.maven.scm.command.update.UpdateScmResult;
032import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
033import org.apache.maven.scm.provider.ScmProviderRepository;
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.apache.maven.scm.provider.svn.svnexe.command.changelog.SvnChangeLogCommand;
039import org.apache.maven.scm.provider.svn.util.SvnUtil;
040import org.apache.maven.scm.providers.svn.settings.Settings;
041import org.codehaus.plexus.util.Os;
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 SvnUpdateCommand extends AbstractUpdateCommand implements SvnCommand {
051    /** {@inheritDoc} */
052    protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version)
053            throws ScmException {
054        Commandline cl = createCommandLine((SvnScmProviderRepository) repo, fileSet.getBasedir(), version);
055
056        SvnUpdateConsumer consumer = new SvnUpdateConsumer(fileSet.getBasedir());
057
058        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
059
060        if (logger.isInfoEnabled()) {
061            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
062
063            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
064                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
065            }
066        }
067
068        int exitCode;
069
070        try {
071            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
072        } catch (CommandLineException ex) {
073            throw new ScmException("Error while executing command.", ex);
074        }
075
076        if (exitCode != 0) {
077            return new UpdateScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
078        }
079
080        UpdateScmResultWithRevision result = new UpdateScmResultWithRevision(
081                cl.toString(), consumer.getUpdatedFiles(), String.valueOf(consumer.getRevision()));
082
083        result.setChanges(consumer.getChangeSets());
084
085        if (logger.isDebugEnabled()) {
086            logger.debug("changeSets " + consumer.getChangeSets());
087        }
088
089        return result;
090    }
091
092    // ----------------------------------------------------------------------
093    //
094    // ----------------------------------------------------------------------
095
096    public static Commandline createCommandLine(
097            SvnScmProviderRepository repository, File workingDirectory, ScmVersion version) {
098        Settings settings = SvnUtil.getSettings();
099
100        String workingDir = workingDirectory.getAbsolutePath();
101
102        if (settings.isUseCygwinPath()) {
103            workingDir = settings.getCygwinMountPath() + "/" + workingDir;
104            workingDir = StringUtils.replace(workingDir, ":", "");
105            workingDir = StringUtils.replace(workingDir, "\\", "/");
106        }
107
108        if (version != null && StringUtils.isEmpty(version.getName())) {
109            version = null;
110        }
111
112        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository);
113
114        if (version == null || SvnTagBranchUtils.isRevisionSpecifier(version)) {
115            cl.createArg().setValue("update");
116
117            if (version != null && StringUtils.isNotEmpty(version.getName())) {
118                cl.createArg().setValue("-r");
119                cl.createArg().setValue(version.getName());
120            }
121
122            cl.createArg().setValue(workingDir + "@");
123        } else {
124            if (version instanceof ScmBranch) {
125                // The tag specified does not appear to be numeric, so assume it refers
126                // to a branch/tag url and perform a switch operation rather than update
127                cl.createArg().setValue("switch");
128                if (version instanceof ScmTag) {
129                    String tagUrl = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
130                    cl.createArg().setValue(tagUrl + "@");
131                } else {
132                    String branchUrl = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
133                    cl.createArg().setValue(branchUrl + "@");
134                }
135                cl.createArg().setValue(workingDir + "@");
136            }
137        }
138
139        return cl;
140    }
141
142    /** {@inheritDoc} */
143    protected ChangeLogCommand getChangeLogCommand() {
144        return new SvnChangeLogCommand();
145    }
146}