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