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                 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 }