001package org.apache.maven.scm.provider.svn.svnexe.command.tag;
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;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.Iterator;
026import java.util.List;
027
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmFileStatus;
032import org.apache.maven.scm.ScmResult;
033import org.apache.maven.scm.ScmTag;
034import org.apache.maven.scm.ScmTagParameters;
035import org.apache.maven.scm.command.tag.AbstractTagCommand;
036import org.apache.maven.scm.command.tag.TagScmResult;
037import org.apache.maven.scm.provider.ScmProviderRepository;
038import org.apache.maven.scm.provider.svn.SvnCommandUtils;
039import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
040import org.apache.maven.scm.provider.svn.command.SvnCommand;
041import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
042import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
043import org.codehaus.plexus.util.FileUtils;
044import org.codehaus.plexus.util.Os;
045import org.codehaus.plexus.util.StringUtils;
046import org.codehaus.plexus.util.cli.CommandLineException;
047import org.codehaus.plexus.util.cli.CommandLineUtils;
048import org.codehaus.plexus.util.cli.Commandline;
049
050/**
051 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
052 * @author Olivier Lamy
053 *
054 * TODO since this is just a copy, use that instead.
055 */
056public class SvnTagCommand
057    extends AbstractTagCommand
058    implements SvnCommand
059{
060
061    public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
062        throws ScmException
063    {
064        ScmTagParameters scmTagParameters = new ScmTagParameters( message );
065        // force false to preserve backward comp
066        scmTagParameters.setRemoteTagging( false );
067        scmTagParameters.setPinExternals( false );
068        return executeTagCommand( repo, fileSet, tag, scmTagParameters );
069    }
070
071    /** {@inheritDoc} */
072    public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
073                                        ScmTagParameters scmTagParameters )
074        throws ScmException
075    {
076        // NPE free
077        if ( scmTagParameters == null )
078        {
079            getLogger().debug( "SvnTagCommand :: scmTagParameters is null create an empty one" );
080            scmTagParameters = new ScmTagParameters();
081            scmTagParameters.setRemoteTagging( false );
082            scmTagParameters.setPinExternals( false );
083        }
084        else
085        {
086            getLogger().debug(
087                               "SvnTagCommand :: scmTagParameters.remoteTagging : "
088                                   + scmTagParameters.isRemoteTagging() );
089        }
090        if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
091        {
092            throw new ScmException( "tag must be specified" );
093        }
094
095        if ( !fileSet.getFileList().isEmpty() )
096        {
097            throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
098        }
099
100        SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
101
102        File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
103
104        try
105        {
106            FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", scmTagParameters.getMessage() );
107        }
108        catch ( IOException ex )
109        {
110            return new TagScmResult( null, "Error while making a temporary file for the commit message: "
111                + ex.getMessage(), null, false );
112        }
113
114        Commandline cl = createCommandLine( repository, fileSet.getBasedir(), tag, messageFile, scmTagParameters );
115
116        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
117
118        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
119
120        if ( getLogger().isInfoEnabled() )
121        {
122            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
123
124            if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
125            {
126                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
127            }
128        }
129
130        int exitCode;
131
132        try
133        {
134            exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
135        }
136        catch ( CommandLineException ex )
137        {
138            throw new ScmException( "Error while executing command.", ex );
139        }
140        finally
141        {
142            try
143            {
144                FileUtils.forceDelete( messageFile );
145            }
146            catch ( IOException ex )
147            {
148                // ignore
149            }
150        }
151
152        if ( exitCode != 0 )
153        {
154            // TODO: Improve this error message
155            return new TagScmResult( cl.toString(), "The svn tag command failed.", stderr.getOutput(), false );
156        }
157
158        List<ScmFile> fileList = new ArrayList<ScmFile>();
159
160        List<File> files = null;
161
162        try
163        {
164            if ( StringUtils.isNotEmpty( fileSet.getExcludes() ) )
165            {
166                files = FileUtils.getFiles( fileSet.getBasedir(),
167                                    ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
168                                                    : fileSet.getIncludes() ), fileSet.getExcludes()
169                                        + ",**/.svn/**", false );
170            }
171            else
172            {
173                files = FileUtils.getFiles( fileSet.getBasedir(),
174                                    ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
175                                                    : fileSet.getIncludes() ), "**/.svn/**", false );
176            }
177        }
178        catch ( IOException e )
179        {
180            throw new ScmException( "Error while executing command.", e );
181        }
182
183        for ( Iterator<File> i = files.iterator(); i.hasNext(); )
184        {
185            File f = i.next();
186
187            fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
188        }
189
190        return new TagScmResult( cl.toString(), fileList );
191    }
192
193    // ----------------------------------------------------------------------
194    //
195    // ----------------------------------------------------------------------
196
197    /**
198     * @deprecated
199     * @param repository
200     * @param workingDirectory
201     * @param tag
202     * @param messageFile
203     * @return TODO
204     */
205    public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, String tag,
206                                                 File messageFile )
207    {
208        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
209
210        cl.createArg().setValue( "copy" );
211
212        cl.createArg().setValue( "--parents" );
213
214        cl.createArg().setValue( "--file" );
215
216        cl.createArg().setValue( messageFile.getAbsolutePath() );
217
218        cl.createArg().setValue( "." );
219
220        // Note: this currently assumes you have the tag base checked out too
221        String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
222        tagUrl = SvnCommandUtils.fixUrl( tagUrl, repository.getUser() );
223        cl.createArg().setValue( tagUrl + "@" );
224
225        return cl;
226    }
227
228
229    public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
230                                                 String tag, File messageFile, ScmTagParameters scmTagParameters )
231    {
232        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
233
234        cl.createArg().setValue( "copy" );
235
236        cl.createArg().setValue( "--file" );
237
238        cl.createArg().setValue( messageFile.getAbsolutePath() );
239
240        cl.createArg().setValue( "--encoding" );
241
242        cl.createArg().setValue( "UTF-8" );
243
244        cl.createArg().setValue( "--parents" );
245
246        if ( scmTagParameters != null && scmTagParameters.getScmRevision() != null )
247        {
248            cl.createArg().setValue( "--revision" );
249
250            cl.createArg().setValue( scmTagParameters.getScmRevision() );
251
252        }
253
254        if ( scmTagParameters != null && scmTagParameters.isPinExternals() )
255        {
256            cl.createArg().setValue( "--pin-externals" );
257        }
258
259        if ( scmTagParameters != null && scmTagParameters.isRemoteTagging() )
260        {
261            String url = SvnCommandUtils.fixUrl( repository.getUrl(), repository.getUser() );
262            cl.createArg().setValue( url + "@" );
263        }
264        else
265        {
266            cl.createArg().setValue( "." );
267        }
268
269        // Note: this currently assumes you have the tag base checked out too
270        String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
271        tagUrl = SvnCommandUtils.fixUrl( tagUrl, repository.getUser() );
272        cl.createArg().setValue( tagUrl + "@" );
273
274        return cl;
275    }
276}