1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
package fr.paris.lutece.plugins.releaser.util.svn; |
35 | |
|
36 | |
import java.io.File; |
37 | |
import java.io.IOException; |
38 | |
import java.io.PrintWriter; |
39 | |
import java.io.StringWriter; |
40 | |
|
41 | |
import org.apache.commons.lang.ObjectUtils; |
42 | |
import org.apache.log4j.Logger; |
43 | |
import org.tmatesoft.svn.core.ISVNDirEntryHandler; |
44 | |
import org.tmatesoft.svn.core.SVNAuthenticationException; |
45 | |
import org.tmatesoft.svn.core.SVNCancelException; |
46 | |
import org.tmatesoft.svn.core.SVNCommitInfo; |
47 | |
import org.tmatesoft.svn.core.SVNDirEntry; |
48 | |
import org.tmatesoft.svn.core.SVNException; |
49 | |
import org.tmatesoft.svn.core.SVNNodeKind; |
50 | |
import org.tmatesoft.svn.core.SVNURL; |
51 | |
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; |
52 | |
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory; |
53 | |
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl; |
54 | |
import org.tmatesoft.svn.core.io.SVNRepository; |
55 | |
import org.tmatesoft.svn.core.io.SVNRepositoryFactory; |
56 | |
import org.tmatesoft.svn.core.wc.ISVNEventHandler; |
57 | |
import org.tmatesoft.svn.core.wc.SVNClientManager; |
58 | |
import org.tmatesoft.svn.core.wc.SVNCommitPacket; |
59 | |
import org.tmatesoft.svn.core.wc.SVNCopyClient; |
60 | |
import org.tmatesoft.svn.core.wc.SVNCopySource; |
61 | |
import org.tmatesoft.svn.core.wc.SVNEvent; |
62 | |
import org.tmatesoft.svn.core.wc.SVNEventAction; |
63 | |
import org.tmatesoft.svn.core.wc.SVNRevision; |
64 | |
|
65 | |
import fr.paris.lutece.plugins.releaser.util.CommandResult; |
66 | |
import fr.paris.lutece.plugins.releaser.util.ConstanteUtils; |
67 | |
import fr.paris.lutece.plugins.releaser.util.ReleaserUtils; |
68 | |
import fr.paris.lutece.plugins.releaser.util.file.FileUtils; |
69 | |
import fr.paris.lutece.util.ReferenceItem; |
70 | |
import fr.paris.lutece.util.ReferenceList; |
71 | |
|
72 | |
|
73 | |
|
74 | |
public final class SvnUtils |
75 | |
{ |
76 | |
private static final String MESSAGE_ERROR_SVN = "Impossible de se connecter au SVN. Veuillez verifier vos identifiants"; |
77 | |
private static final String CONSTANTE_SLASH = "/"; |
78 | 0 | private static final Logger logger = Logger.getLogger( SvnUtils.class ); |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private SvnUtils( ) |
84 | 0 | { |
85 | |
|
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public static void init( ) |
92 | |
{ |
93 | |
|
94 | |
|
95 | |
|
96 | 0 | DAVRepositoryFactory.setup( ); |
97 | |
|
98 | |
|
99 | |
|
100 | 0 | SVNRepositoryFactoryImpl.setup( ); |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | 0 | FSRepositoryFactory.setup( ); |
106 | 0 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public static void doCommit( String strPathFile,String strCommitMessage, |
118 | |
ReleaseSvnCommitClient commitClient ) throws SVNException |
119 | |
{ |
120 | 0 | SVNCommitPacket commitPacket = commitClient.doCollectCommitItems( new File[] { new File( strPathFile ) }, |
121 | |
false, false, false ); |
122 | |
|
123 | 0 | if ( !SVNCommitPacket.EMPTY.equals( commitPacket ) ) |
124 | |
{ |
125 | 0 | commitClient.doCommit( commitPacket, false, strCommitMessage ); |
126 | |
} |
127 | 0 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public static String doTagSite( String strSiteName, String strTagName, String strSrcURL, String strDstURL, |
138 | |
SVNCopyClient copyClient ) throws SVNException |
139 | |
{ |
140 | |
|
141 | 0 | SVNURL srcURL = SVNURL.parseURIEncoded( strSrcURL ); |
142 | 0 | SVNURL dstURL = SVNURL.parseURIEncoded( strDstURL ); |
143 | 0 | SVNCopySource svnCopySource = new SVNCopySource( SVNRevision.HEAD, SVNRevision.HEAD, srcURL ); |
144 | 0 | SVNCopySource[] tabSVNCopy = new SVNCopySource[1]; |
145 | 0 | tabSVNCopy[0] = svnCopySource; |
146 | |
|
147 | 0 | SVNCommitInfo info = copyClient.doCopy( tabSVNCopy, dstURL, false, false, false, |
148 | |
"[site-release] Tag site " + strSiteName + " to " + strTagName, null ); |
149 | |
|
150 | 0 | if ( info.getErrorMessage( ) != null ) |
151 | |
{ |
152 | |
|
153 | 0 | return info.getErrorMessage( ).getMessage( ); |
154 | |
} |
155 | |
|
156 | 0 | return null; |
157 | |
} |
158 | |
|
159 | |
public static String doSvnCheckout( String strUrl, String strCheckoutBaseSitePath, |
160 | |
ReleaseSvnCheckoutClient updateClient, CommandResult result ) |
161 | |
throws SVNException |
162 | |
{ |
163 | 0 | SVNURL url = SVNURL.parseURIEncoded( strUrl ); |
164 | 0 | File file = new File( strCheckoutBaseSitePath ); |
165 | |
|
166 | 0 | if ( file.exists( ) ) |
167 | |
{ |
168 | 0 | if ( !FileUtils.delete( file, result.getLog( ) ) ) |
169 | |
{ |
170 | 0 | result.setError( result.getLog( ).toString( ) ); |
171 | |
|
172 | 0 | return result.getLog( ).toString( ); |
173 | |
} |
174 | |
} |
175 | |
|
176 | 0 | SVNRepository repository = SVNRepositoryFactory.create( url, null ); |
177 | 0 | final StringBuffer logBuffer = result.getLog( ); |
178 | |
|
179 | |
try |
180 | |
{ |
181 | 0 | updateClient.setEventHandler( new ISVNEventHandler( ) |
182 | 0 | { |
183 | |
public void checkCancelled( ) throws SVNCancelException |
184 | |
{ |
185 | |
|
186 | 0 | } |
187 | |
|
188 | |
public void handleEvent( SVNEvent event, double progress ) |
189 | |
throws SVNException |
190 | |
{ |
191 | 0 | logBuffer.append( ( ( event.getAction( ) == SVNEventAction.UPDATE_ADD ) ? "ADDED " |
192 | 0 | : event.getAction( ) ) + |
193 | 0 | " " + event.getFile( ) + "\n" ); |
194 | 0 | } |
195 | |
} ); |
196 | |
|
197 | |
|
198 | 0 | updateClient.doCheckout( repository.getLocation( ), file, SVNRevision.HEAD, SVNRevision.HEAD, true ); |
199 | |
} |
200 | 0 | catch ( SVNAuthenticationException e ) |
201 | |
{ |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | 0 | ReleaserUtils.addTechnicalError(result,"Une erreur est survenue lors de la tentative d'authentification avec le svn"+e,e); |
208 | |
|
209 | 0 | StringWriter sw = new StringWriter( ); |
210 | 0 | PrintWriter pw = new PrintWriter( sw ); |
211 | 0 | e.printStackTrace( pw ); |
212 | |
|
213 | 0 | String errorLog = sw.toString( ); |
214 | 0 | pw.flush( ); |
215 | 0 | pw.close( ); |
216 | |
|
217 | |
try |
218 | |
{ |
219 | 0 | sw.flush( ); |
220 | 0 | sw.close( ); |
221 | |
} |
222 | 0 | catch ( IOException e1 ) |
223 | |
{ |
224 | |
|
225 | |
|
226 | 0 | } |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
} |
233 | 0 | catch ( Exception e ) |
234 | |
{ |
235 | |
|
236 | |
|
237 | 0 | StringWriter sw = new StringWriter( ); |
238 | 0 | PrintWriter pw = new PrintWriter( sw ); |
239 | 0 | e.printStackTrace( pw ); |
240 | |
|
241 | 0 | String errorLog = sw.toString( ); |
242 | 0 | pw.flush( ); |
243 | 0 | pw.close( ); |
244 | |
|
245 | |
try |
246 | |
{ |
247 | 0 | sw.flush( ); |
248 | 0 | sw.close( ); |
249 | |
} |
250 | 0 | catch ( IOException e1 ) |
251 | |
{ |
252 | |
|
253 | |
|
254 | 0 | } |
255 | |
|
256 | 0 | ReleaserUtils.addTechnicalError(result,"Une erreur svn est survenue:"+e,e); |
257 | |
|
258 | 0 | } |
259 | |
|
260 | 0 | return null; |
261 | |
} |
262 | |
|
263 | |
public static ReferenceList getSvnSites( String strUrlSite, SVNClientManager clientManager ) |
264 | |
throws SVNException |
265 | |
{ |
266 | 0 | final ReferenceList listSites = new ReferenceList( ); |
267 | |
final SVNURL url; |
268 | |
|
269 | 0 | url = SVNURL.parseURIEncoded( strUrlSite ); |
270 | |
|
271 | 0 | SVNRepository repository = SVNRepositoryFactory.create( url, null ); |
272 | |
|
273 | 0 | clientManager.getLogClient( ).doList( repository.getLocation( ), SVNRevision.HEAD, SVNRevision.HEAD, false, |
274 | |
false, |
275 | |
new ISVNDirEntryHandler( ) |
276 | 0 | { |
277 | |
public void handleDirEntry( SVNDirEntry entry ) |
278 | |
throws SVNException |
279 | |
{ |
280 | |
|
281 | 0 | if ( !url.equals( entry.getURL( ) ) ) |
282 | |
{ |
283 | 0 | if ( entry.getKind( ) == SVNNodeKind.DIR ) |
284 | |
{ |
285 | 0 | listSites.addItem( entry.getName( ), entry.getName( ) ); |
286 | |
} |
287 | |
} |
288 | 0 | } |
289 | |
} ); |
290 | |
|
291 | 0 | return listSites; |
292 | |
} |
293 | |
|
294 | |
|
295 | |
public static String getSvnUrlTagSite( String strScmUrl, String strTagName ) |
296 | |
{ |
297 | |
|
298 | 0 | String strUrl=strScmUrl.contains( ConstanteUtils.CONSTANTE_TRUNK )?strScmUrl.replace( ConstanteUtils.CONSTANTE_TRUNK, ConstanteUtils.CONSTANTE_TAGS ):strScmUrl; |
299 | 0 | return strUrl+ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strTagName; |
300 | |
} |
301 | |
|
302 | |
|
303 | |
public static String getRepoUrl(String strRepoUrl) |
304 | |
{ |
305 | |
|
306 | 0 | if(strRepoUrl!=null && strRepoUrl.startsWith( "scm:svn:" )) |
307 | |
{ |
308 | 0 | strRepoUrl=strRepoUrl.substring( 8 ); |
309 | |
|
310 | |
|
311 | |
} |
312 | |
|
313 | 0 | return strRepoUrl; |
314 | |
|
315 | |
|
316 | |
} |
317 | |
|
318 | |
|
319 | |
|
320 | |
} |