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          scmTagParameters.setPinExternals( false );
68          return executeTagCommand( repo, fileSet, tag, scmTagParameters );
69      }
70  
71      /** {@inheritDoc} */
72      public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
73                                          ScmTagParameters scmTagParameters )
74          throws ScmException
75      {
76          // NPE free
77          if ( scmTagParameters == null )
78          {
79              getLogger().debug( "SvnTagCommand :: scmTagParameters is null create an empty one" );
80              scmTagParameters = new ScmTagParameters();
81              scmTagParameters.setRemoteTagging( false );
82              scmTagParameters.setPinExternals( false );
83          }
84          else
85          {
86              getLogger().debug(
87                                 "SvnTagCommand :: scmTagParameters.remoteTagging : "
88                                     + scmTagParameters.isRemoteTagging() );
89          }
90          if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
91          {
92              throw new ScmException( "tag must be specified" );
93          }
94  
95          if ( !fileSet.getFileList().isEmpty() )
96          {
97              throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
98          }
99  
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                 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                 List<File> list =
176                     FileUtils.getFiles( fileSet.getBasedir(),
177                                         ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
178                                                         : fileSet.getIncludes() ), "**/.svn/**", false );
179                 files = list;
180             }
181         }
182         catch ( IOException e )
183         {
184             throw new ScmException( "Error while executing command.", e );
185         }
186 
187         for ( Iterator<File> i = files.iterator(); i.hasNext(); )
188         {
189             File f = i.next();
190 
191             fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
192         }
193 
194         return new TagScmResult( cl.toString(), fileList );
195     }
196 
197     // ----------------------------------------------------------------------
198     //
199     // ----------------------------------------------------------------------
200 
201     /**
202      * @deprecated
203      * @param repository
204      * @param workingDirectory
205      * @param tag
206      * @param messageFile
207      * @return
208      */
209     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, String tag,
210                                                  File messageFile )
211     {
212         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
213 
214         cl.createArg().setValue( "copy" );
215 
216         cl.createArg().setValue( "--parents" );
217 
218         cl.createArg().setValue( "--file" );
219 
220         cl.createArg().setValue( messageFile.getAbsolutePath() );
221 
222         cl.createArg().setValue( "." );
223 
224         // Note: this currently assumes you have the tag base checked out too
225         String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
226         tagUrl = SvnCommandUtils.fixUrl( tagUrl, repository.getUser() );
227         cl.createArg().setValue( tagUrl + "@" );
228 
229         return cl;
230     }
231 
232 
233     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
234                                                  String tag, File messageFile, ScmTagParameters scmTagParameters )
235     {
236         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
237 
238         cl.createArg().setValue( "copy" );
239 
240         cl.createArg().setValue( "--file" );
241 
242         cl.createArg().setValue( messageFile.getAbsolutePath() );
243 
244         cl.createArg().setValue( "--encoding" );
245 
246         cl.createArg().setValue( "UTF-8" );
247 
248         cl.createArg().setValue( "--parents" );
249 
250         if ( scmTagParameters != null && scmTagParameters.getScmRevision() != null )
251         {
252             cl.createArg().setValue( "--revision" );
253 
254             cl.createArg().setValue( scmTagParameters.getScmRevision() );
255 
256         }
257 
258         if ( scmTagParameters != null && scmTagParameters.isPinExternals() )
259         {
260             cl.createArg().setValue( "--pin-externals" );
261         }
262 
263         if ( scmTagParameters != null && scmTagParameters.isRemoteTagging() )
264         {
265             String url = SvnCommandUtils.fixUrl( repository.getUrl(), repository.getUser() );
266             cl.createArg().setValue( url + "@" );
267         }
268         else
269         {
270             cl.createArg().setValue( "." );
271         }
272 
273         // Note: this currently assumes you have the tag base checked out too
274         String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
275         tagUrl = SvnCommandUtils.fixUrl( tagUrl, repository.getUser() );
276         cl.createArg().setValue( tagUrl + "@" );
277 
278         return cl;
279     }
280 }