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 org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFile;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.ScmFileStatus;
26 import org.apache.maven.scm.ScmResult;
27 import org.apache.maven.scm.ScmTag;
28 import org.apache.maven.scm.command.tag.AbstractTagCommand;
29 import org.apache.maven.scm.command.tag.TagScmResult;
30 import org.apache.maven.scm.provider.ScmProviderRepository;
31 import org.apache.maven.scm.provider.svn.SvnCommandUtils;
32 import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
33 import org.apache.maven.scm.provider.svn.command.SvnCommand;
34 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
35 import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
36 import org.codehaus.plexus.util.FileUtils;
37 import org.codehaus.plexus.util.StringUtils;
38 import org.codehaus.plexus.util.cli.CommandLineException;
39 import org.codehaus.plexus.util.cli.CommandLineUtils;
40 import org.codehaus.plexus.util.cli.Commandline;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.util.ArrayList;
45 import java.util.Iterator;
46 import java.util.List;
47
48
49
50
51
52
53 public class SvnTagCommand
54 extends AbstractTagCommand
55 implements SvnCommand
56 {
57 public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
58 throws ScmException
59 {
60 if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
61 {
62 throw new ScmException( "tag must be specified" );
63 }
64
65 if ( fileSet.getFiles().length != 0 )
66 {
67 throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
68 }
69
70 SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
71
72 File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
73
74 try
75 {
76 FileUtils.fileWrite( messageFile.getAbsolutePath(), message );
77 }
78 catch ( IOException ex )
79 {
80 return new TagScmResult( null,
81 "Error while making a temporary file for the commit message: " + ex.getMessage(),
82 null, false );
83 }
84
85 Commandline cl = createCommandLine( repository, fileSet.getBasedir(), tag, messageFile );
86
87 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
88
89 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
90
91 getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
92 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
93
94 int exitCode;
95
96 try
97 {
98 exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
99 }
100 catch ( CommandLineException ex )
101 {
102 throw new ScmException( "Error while executing command.", ex );
103 }
104 finally
105 {
106 try
107 {
108 FileUtils.forceDelete( messageFile );
109 }
110 catch ( IOException ex )
111 {
112
113 }
114 }
115
116 if ( exitCode != 0 )
117 {
118
119 return new TagScmResult( cl.toString(), "The svn tag command failed.", stderr.getOutput(), false );
120 }
121
122 List fileList = new ArrayList();
123
124 List files = null;
125
126 try
127 {
128 files = FileUtils.getFiles( fileSet.getBasedir(), "**", "**/.svn/**", false );
129 }
130 catch ( IOException e )
131 {
132 throw new ScmException( "Error while executing command.", e );
133 }
134
135 for ( Iterator i = files.iterator(); i.hasNext(); )
136 {
137 File f = (File) i.next();
138
139 fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
140 }
141
142 return new TagScmResult( cl.toString(), fileList );
143 }
144
145
146
147
148
149 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, String tag,
150 File messageFile )
151 {
152 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
153
154 cl.createArgument().setValue( "copy" );
155
156 cl.createArgument().setValue( "--file" );
157
158 cl.createArgument().setValue( messageFile.getAbsolutePath() );
159
160 cl.createArgument().setValue( "." );
161
162
163 String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) );
164 cl.createArgument().setValue( SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ) );
165
166 return cl;
167 }
168 }