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.StringUtils;
45  import org.codehaus.plexus.util.cli.CommandLineException;
46  import org.codehaus.plexus.util.cli.CommandLineUtils;
47  import org.codehaus.plexus.util.cli.Commandline;
48  
49  /**
50   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
51   * @author Olivier Lamy
52   *
53   * @todo since this is just a copy, use that instead.
54   */
55  public class SvnTagCommand
56      extends AbstractTagCommand
57      implements SvnCommand
58  {
59      
60      public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
61          throws ScmException
62      {
63          ScmTagParameters scmTagParameters = new ScmTagParameters( message );
64          // force false to preserve backward comp
65          scmTagParameters.setRemoteTagging( false );
66          return executeTagCommand( repo, fileSet, tag, scmTagParameters );
67      }
68      
69      /** {@inheritDoc} */
70      public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
71                                          ScmTagParameters scmTagParameters )
72          throws ScmException
73      {
74          // NPE free
75          if (scmTagParameters == null)
76          {
77              getLogger().debug( "SvnTagCommand :: scmTagParameters is null create an empty one" );
78              scmTagParameters = new ScmTagParameters();
79              scmTagParameters.setRemoteTagging( false );
80            
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             getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
123         }
124 
125         int exitCode;
126 
127         try
128         {
129             exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
130         }
131         catch ( CommandLineException ex )
132         {
133             throw new ScmException( "Error while executing command.", ex );
134         }
135         finally
136         {
137             try
138             {
139                 FileUtils.forceDelete( messageFile );
140             }
141             catch ( IOException ex )
142             {
143                 // ignore
144             }
145         }
146 
147         if ( exitCode != 0 )
148         {
149             // TODO: Improve this error message
150             return new TagScmResult( cl.toString(), "The svn tag command failed.", stderr.getOutput(), false );
151         }
152 
153         List<ScmFile> fileList = new ArrayList<ScmFile>();
154 
155         List<File> files = null;
156 
157         try
158         {
159             if ( StringUtils.isNotEmpty( fileSet.getExcludes() ) )
160             {
161                 @SuppressWarnings( "unchecked" )
162                 List<File> list =
163                     FileUtils.getFiles( fileSet.getBasedir(),
164                                         ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
165                                                         : fileSet.getIncludes() ), fileSet.getExcludes()
166                                             + ",**/.svn/**", false );
167                 files = list;
168             }
169             else
170             {
171                 @SuppressWarnings( "unchecked" )
172                 List<File> list =
173                     FileUtils.getFiles( fileSet.getBasedir(),
174                                         ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
175                                                         : fileSet.getIncludes() ), "**/.svn/**", false );
176                 files = list;
177             }
178         }
179         catch ( IOException e )
180         {
181             throw new ScmException( "Error while executing command.", e );
182         }
183 
184         for ( Iterator<File> i = files.iterator(); i.hasNext(); )
185         {
186             File f = i.next();
187 
188             fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
189         }
190 
191         return new TagScmResult( cl.toString(), fileList );
192     }
193 
194     // ----------------------------------------------------------------------
195     //
196     // ----------------------------------------------------------------------
197 
198     /**
199      * @deprecated
200      * @param repository
201      * @param workingDirectory
202      * @param tag
203      * @param messageFile
204      * @return
205      */
206     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, String tag,
207                                                  File messageFile )
208     {
209         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
210 
211         cl.createArg().setValue( "copy" );
212 
213         cl.createArg().setValue( "--file" );
214 
215         cl.createArg().setValue( messageFile.getAbsolutePath() );
216 
217         cl.createArg().setValue( "." );
218 
219         // Note: this currently assumes you have the tag base checked out too
220         String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
221         cl.createArg().setValue( SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ) );
222 
223         return cl;
224     }
225 
226     
227     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
228                                                  String tag, File messageFile, ScmTagParameters scmTagParameters )
229     {
230         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
231 
232         cl.createArg().setValue( "copy" );
233 
234         cl.createArg().setValue( "--file" );
235 
236         cl.createArg().setValue( messageFile.getAbsolutePath() );
237 
238         // SCM-487 olamy : this need a svn 1.5 cli 
239         //cl.createArg().setValue( "--parents" );
240         
241         if ( scmTagParameters != null && scmTagParameters.getScmRevision() != null )
242         {
243             cl.createArg().setValue( "--revision" );
244             
245             cl.createArg().setValue( scmTagParameters.getScmRevision() );
246             
247         }
248         
249 
250         if ( scmTagParameters != null && scmTagParameters.isRemoteTagging() )
251         {
252             cl.createArg().setValue( SvnCommandUtils.fixUrl( repository.getUrl(), repository.getUser() ) );
253         }
254         else
255         {
256             cl.createArg().setValue( "." );
257         }
258 
259         // Note: this currently assumes you have the tag base checked out too
260         String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
261         cl.createArg().setValue( SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ) );
262 
263         return cl;
264     }    
265 }