View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.tag;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFile;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmFileStatus;
32  import org.apache.maven.scm.ScmResult;
33  import org.apache.maven.scm.ScmTag;
34  import org.apache.maven.scm.ScmTagParameters;
35  import org.apache.maven.scm.command.tag.AbstractTagCommand;
36  import org.apache.maven.scm.command.tag.TagScmResult;
37  import org.apache.maven.scm.provider.ScmProviderRepository;
38  import org.apache.maven.scm.provider.svn.SvnCommandUtils;
39  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
40  import org.apache.maven.scm.provider.svn.command.SvnCommand;
41  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
42  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
43  import org.codehaus.plexus.util.FileUtils;
44  import org.codehaus.plexus.util.Os;
45  import org.codehaus.plexus.util.StringUtils;
46  import org.codehaus.plexus.util.cli.CommandLineException;
47  import org.codehaus.plexus.util.cli.CommandLineUtils;
48  import org.codehaus.plexus.util.cli.Commandline;
49  
50  /**
51   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
52   * @author Olivier Lamy
53   *
54   * @todo since this is just a copy, use that instead.
55   */
56  public class SvnTagCommand
57      extends AbstractTagCommand
58      implements SvnCommand
59  {
60  
61      public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
62          throws ScmException
63      {
64          ScmTagParameters scmTagParameters = new ScmTagParameters( message );
65          // force false to preserve backward comp
66          scmTagParameters.setRemoteTagging( false );
67          return executeTagCommand( repo, fileSet, tag, scmTagParameters );
68      }
69  
70      /** {@inheritDoc} */
71      public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
72                                          ScmTagParameters scmTagParameters )
73          throws ScmException
74      {
75          // NPE free
76          if ( scmTagParameters == null )
77          {
78              getLogger().debug( "SvnTagCommand :: scmTagParameters is null create an empty one" );
79              scmTagParameters = new ScmTagParameters();
80              scmTagParameters.setRemoteTagging( false );
81          }
82          else
83          {
84              getLogger().debug(
85                                 "SvnTagCommand :: scmTagParameters.remoteTagging : "
86                                     + scmTagParameters.isRemoteTagging() );
87          }
88          if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
89          {
90              throw new ScmException( "tag must be specified" );
91          }
92  
93          if ( !fileSet.getFileList().isEmpty() )
94          {
95              throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
96          }
97  
98          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
99  
100         File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
101 
102         try
103         {
104             FileUtils.fileWrite( messageFile.getAbsolutePath(), scmTagParameters == null ? "" : scmTagParameters
105                 .getMessage() );
106         }
107         catch ( IOException ex )
108         {
109             return new TagScmResult( null, "Error while making a temporary file for the commit message: "
110                 + ex.getMessage(), null, false );
111         }
112 
113         Commandline cl = createCommandLine( repository, fileSet.getBasedir(), tag, messageFile, scmTagParameters );
114 
115         CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
116 
117         CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
118 
119         if ( getLogger().isInfoEnabled() )
120         {
121             getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
122 
123             if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
124             {
125                 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
126             }
127         }
128 
129         int exitCode;
130 
131         try
132         {
133             exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
134         }
135         catch ( CommandLineException ex )
136         {
137             throw new ScmException( "Error while executing command.", ex );
138         }
139         finally
140         {
141             try
142             {
143                 FileUtils.forceDelete( messageFile );
144             }
145             catch ( IOException ex )
146             {
147                 // ignore
148             }
149         }
150 
151         if ( exitCode != 0 )
152         {
153             // TODO: Improve this error message
154             return new TagScmResult( cl.toString(), "The svn tag command failed.", stderr.getOutput(), false );
155         }
156 
157         List<ScmFile> fileList = new ArrayList<ScmFile>();
158 
159         List<File> files = null;
160 
161         try
162         {
163             if ( StringUtils.isNotEmpty( fileSet.getExcludes() ) )
164             {
165                 @SuppressWarnings( "unchecked" )
166                 List<File> list =
167                     FileUtils.getFiles( fileSet.getBasedir(),
168                                         ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
169                                                         : fileSet.getIncludes() ), fileSet.getExcludes()
170                                             + ",**/.svn/**", false );
171                 files = list;
172             }
173             else
174             {
175                 @SuppressWarnings( "unchecked" )
176                 List<File> list =
177                     FileUtils.getFiles( fileSet.getBasedir(),
178                                         ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
179                                                         : fileSet.getIncludes() ), "**/.svn/**", false );
180                 files = list;
181             }
182         }
183         catch ( IOException e )
184         {
185             throw new ScmException( "Error while executing command.", e );
186         }
187 
188         for ( Iterator<File> i = files.iterator(); i.hasNext(); )
189         {
190             File f = i.next();
191 
192             fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
193         }
194 
195         return new TagScmResult( cl.toString(), fileList );
196     }
197 
198     // ----------------------------------------------------------------------
199     //
200     // ----------------------------------------------------------------------
201 
202     /**
203      * @deprecated
204      * @param repository
205      * @param workingDirectory
206      * @param tag
207      * @param messageFile
208      * @return
209      */
210     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, String tag,
211                                                  File messageFile )
212     {
213         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
214 
215         cl.createArg().setValue( "copy" );
216 
217         cl.createArg().setValue( "--file" );
218 
219         cl.createArg().setValue( messageFile.getAbsolutePath() );
220 
221         cl.createArg().setValue( "." );
222 
223         // Note: this currently assumes you have the tag base checked out too
224         String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
225         cl.createArg().setValue( SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ) );
226 
227         return cl;
228     }
229 
230 
231     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
232                                                  String tag, File messageFile, ScmTagParameters scmTagParameters )
233     {
234         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
235 
236         cl.createArg().setValue( "copy" );
237 
238         cl.createArg().setValue( "--file" );
239 
240         cl.createArg().setValue( messageFile.getAbsolutePath() );
241 
242         cl.createArg().setValue( "--parents" );
243 
244         if ( scmTagParameters != null && scmTagParameters.getScmRevision() != null )
245         {
246             cl.createArg().setValue( "--revision" );
247 
248             cl.createArg().setValue( scmTagParameters.getScmRevision() );
249 
250         }
251 
252 
253         if ( scmTagParameters != null && scmTagParameters.isRemoteTagging() )
254         {
255             cl.createArg().setValue( SvnCommandUtils.fixUrl( repository.getUrl(), repository.getUser() ) );
256         }
257         else
258         {
259             cl.createArg().setValue( "." );
260         }
261 
262         // Note: this currently assumes you have the tag base checked out too
263         String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
264         cl.createArg().setValue( SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ) );
265 
266         return cl;
267     }
268 }