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