1 package org.apache.maven.scm.provider.svn.svnexe.command.tag;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
51
52
53
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
65 scmTagParameters.setRemoteTagging( false );
66 return executeTagCommand( repo, fileSet, tag, scmTagParameters );
67 }
68
69
70 public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
71 ScmTagParameters scmTagParameters )
72 throws ScmException
73 {
74
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 else
82 {
83 getLogger().debug(
84 "SvnTagCommand :: scmTagParameters.remoteTagging : "
85 + scmTagParameters.isRemoteTagging() );
86 }
87 if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
88 {
89 throw new ScmException( "tag must be specified" );
90 }
91
92 if ( !fileSet.getFileList().isEmpty() )
93 {
94 throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
95 }
96
97 SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
98
99 File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
100
101 try
102 {
103 FileUtils.fileWrite( messageFile.getAbsolutePath(), scmTagParameters == null ? "" : scmTagParameters
104 .getMessage() );
105 }
106 catch ( IOException ex )
107 {
108 return new TagScmResult( null, "Error while making a temporary file for the commit message: "
109 + ex.getMessage(), null, false );
110 }
111
112 Commandline cl = createCommandLine( repository, fileSet.getBasedir(), tag, messageFile, scmTagParameters );
113
114 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
115
116 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
117
118 if ( getLogger().isInfoEnabled() )
119 {
120 getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
121 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
122 }
123
124 int exitCode;
125
126 try
127 {
128 exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
129 }
130 catch ( CommandLineException ex )
131 {
132 throw new ScmException( "Error while executing command.", ex );
133 }
134 finally
135 {
136 try
137 {
138 FileUtils.forceDelete( messageFile );
139 }
140 catch ( IOException ex )
141 {
142
143 }
144 }
145
146 if ( exitCode != 0 )
147 {
148
149 return new TagScmResult( cl.toString(), "The svn tag command failed.", stderr.getOutput(), false );
150 }
151
152 List<ScmFile> fileList = new ArrayList<ScmFile>();
153
154 List<File> files = null;
155
156 try
157 {
158 if ( StringUtils.isNotEmpty( fileSet.getExcludes() ) )
159 {
160 @SuppressWarnings( "unchecked" )
161 List<File> list =
162 FileUtils.getFiles( fileSet.getBasedir(),
163 ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
164 : fileSet.getIncludes() ), fileSet.getExcludes()
165 + ",**/.svn/**", false );
166 files = list;
167 }
168 else
169 {
170 @SuppressWarnings( "unchecked" )
171 List<File> list =
172 FileUtils.getFiles( fileSet.getBasedir(),
173 ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**"
174 : fileSet.getIncludes() ), "**/.svn/**", false );
175 files = list;
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
199
200
201
202
203
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( "--file" );
213
214 cl.createArg().setValue( messageFile.getAbsolutePath() );
215
216 cl.createArg().setValue( "." );
217
218
219 String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
220 cl.createArg().setValue( SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ) );
221
222 return cl;
223 }
224
225
226 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
227 String tag, File messageFile, ScmTagParameters scmTagParameters )
228 {
229 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
230
231 cl.createArg().setValue( "copy" );
232
233 cl.createArg().setValue( "--file" );
234
235 cl.createArg().setValue( messageFile.getAbsolutePath() );
236
237 cl.createArg().setValue( "--parents" );
238
239 if ( scmTagParameters != null && scmTagParameters.getScmRevision() != null )
240 {
241 cl.createArg().setValue( "--revision" );
242
243 cl.createArg().setValue( scmTagParameters.getScmRevision() );
244
245 }
246
247
248 if ( scmTagParameters != null && scmTagParameters.isRemoteTagging() )
249 {
250 cl.createArg().setValue( SvnCommandUtils.fixUrl( repository.getUrl(), repository.getUser() ) );
251 }
252 else
253 {
254 cl.createArg().setValue( "." );
255 }
256
257
258 String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
259 cl.createArg().setValue( SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ) );
260
261 return cl;
262 }
263 }