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        getLogger().info( "changeSets " + consumer.getChangeSets());
091        
092        return result;
093    }
094
095    // ----------------------------------------------------------------------
096    //
097    // ----------------------------------------------------------------------
098
099    public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
100                                                 ScmVersion version )
101    {
102        Settings settings = SvnUtil.getSettings();
103
104        String workingDir = workingDirectory.getAbsolutePath();
105
106        if ( settings.isUseCygwinPath() )
107        {
108            workingDir = settings.getCygwinMountPath() + "/" + workingDir;
109            workingDir = StringUtils.replace( workingDir, ":", "" );
110            workingDir = StringUtils.replace( workingDir, "\\", "/" );
111        }
112
113        if ( version != null && StringUtils.isEmpty( version.getName() ) )
114        {
115            version = null;
116        }
117
118        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
119
120        if ( version == null || SvnTagBranchUtils.isRevisionSpecifier( version ) )
121        {
122            cl.createArg().setValue( "update" );
123
124            if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
125            {
126                cl.createArg().setValue( "-r" );
127                cl.createArg().setValue( version.getName() );
128            }
129
130            cl.createArg().setValue( workingDir );
131        }
132        else
133        {
134            if ( version instanceof ScmBranch )
135            {
136                // The tag specified does not appear to be numeric, so assume it refers
137                // to a branch/tag url and perform a switch operation rather than update
138                cl.createArg().setValue( "switch" );
139                if ( version instanceof ScmTag )
140                {
141                    cl.createArg().setValue( SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version ) );
142                }
143                else
144                {
145                    cl.createArg().setValue(
146                        SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version ) );
147                }
148                cl.createArg().setValue( workingDir );
149            }
150        }
151
152        return cl;
153    }
154
155    /** {@inheritDoc} */
156    protected ChangeLogCommand getChangeLogCommand()
157    {
158        SvnChangeLogCommand command = new SvnChangeLogCommand();
159
160        command.setLogger( getLogger() );
161
162        return command;
163    }
164
165
166}