1 package org.apache.maven.scm.provider.accurev;
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.util.Date;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import org.apache.maven.scm.ScmVersion;
30 import org.apache.maven.scm.log.ScmLogger;
31 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
32 import org.apache.maven.scm.provider.accurev.util.WorkspaceUtils;
33 import org.codehaus.plexus.util.StringUtils;
34
35 public class AccuRevScmProviderRepository
36 extends ScmProviderRepositoryWithHost
37 {
38 public static final String DEFAULT_TAG_FORMAT = "%s";
39
40 private AccuRev accurev;
41
42 private String streamName;
43
44 private String projectPath;
45
46 private String tagFormat = DEFAULT_TAG_FORMAT;
47
48 private ScmLogger logger;
49
50 public AccuRevScmProviderRepository()
51 {
52 super();
53
54
55 setPersistCheckout( true );
56
57 setShouldUseExportForNonPersistentCheckout( true );
58 }
59
60 public String getTagFormat()
61 {
62 return tagFormat;
63 }
64
65 public void setTagFormat( String tagFormat )
66 {
67 if ( tagFormat == null || !tagFormat.contains( "%s" ) )
68 {
69 throw new IllegalArgumentException( "tagFormat must contain '%s' to be replaced" );
70 }
71 this.tagFormat = tagFormat;
72 }
73
74 public String getStreamName()
75 {
76 return streamName;
77 }
78
79 public void setStreamName( String streamName )
80 {
81 this.streamName = streamName;
82 }
83
84 public String getProjectPath()
85 {
86 return projectPath;
87 }
88
89 public void setProjectPath( String projectPath )
90 {
91 this.projectPath = projectPath;
92 setCheckoutRelativePath( projectPath );
93 }
94
95 public AccuRev getAccuRev()
96 {
97 return this.accurev;
98 }
99
100 public void setAccuRev( AccuRev accurev )
101 {
102 this.accurev = accurev;
103 }
104
105
106
107
108
109 public boolean isWorkSpaceRoot( AccuRevInfo info )
110 {
111 return ( ( getProjectPath() != null && WorkspaceUtils.isSameFile(info.getBasedir(), new File( info.getTop(), getProjectPath() ) ) ) || isWorkSpaceTop( info ) );
112 }
113
114 public boolean isWorkSpaceTop( AccuRevInfo info )
115 {
116 return info.isWorkSpaceTop();
117
118 }
119
120
121 String tagToStream( String tagName )
122 {
123 return String.format( getTagFormat(), tagName );
124 }
125
126 String streamToTag( String streamName )
127 {
128 tagFormat = getTagFormat();
129
130 String tagPatternString = tagToStream( "(.*)" );
131 Pattern tagPattern = Pattern.compile( tagPatternString );
132
133 Matcher tagMatcher = tagPattern.matcher( streamName );
134 if ( tagMatcher.matches() )
135 {
136 return tagMatcher.group( 1 );
137 }
138 else
139 {
140 return streamName;
141 }
142
143 }
144
145 public void setLogger( ScmLogger logger )
146 {
147 this.logger = logger;
148 }
149
150
151
152 private String checkoutRelativePath;
153
154 private boolean shouldUseExportForNonPersistentCheckout = true;
155
156
157
158
159
160
161
162
163
164
165
166 public String getCheckoutRelativePath()
167 {
168 if ( this.checkoutRelativePath == null )
169 {
170 return "";
171 }
172 return this.checkoutRelativePath;
173 }
174
175 public void setCheckoutRelativePath( String checkoutRelativePath )
176 {
177 this.checkoutRelativePath = checkoutRelativePath;
178 }
179
180
181
182
183
184
185 public String getExportRelativePath()
186 {
187 return getCheckoutRelativePath();
188 }
189
190
191
192
193
194
195
196
197
198
199
200 public boolean shouldUseExportForNonPersistentCheckout()
201 {
202 return this.shouldUseExportForNonPersistentCheckout;
203 }
204
205 public void setShouldUseExportForNonPersistentCheckout( boolean shouldUseExportForNonPersistentCheckout )
206 {
207 this.shouldUseExportForNonPersistentCheckout = shouldUseExportForNonPersistentCheckout;
208 }
209
210 public String getDepotRelativeProjectPath()
211 {
212 return "/./" + ( projectPath == null ? "" : projectPath );
213 }
214
215 public AccuRevVersion getAccuRevVersion( ScmVersion scmVersion )
216 {
217
218 String tran = null;
219 String basisStream = null;
220
221 if ( scmVersion == null )
222 {
223 basisStream = getStreamName();
224 }
225 else
226 {
227 String name = StringUtils.clean( scmVersion.getName() );
228
229 String[] versionComponents = name.split( "[/\\\\]", 2 );
230 basisStream = versionComponents[0];
231 if ( basisStream.length() == 0 )
232 {
233
234 basisStream = getStreamName();
235 }
236 else
237 {
238
239 basisStream = tagToStream( basisStream );
240 }
241
242 if ( versionComponents.length == 2 && versionComponents[1].length() > 0 )
243 {
244 tran = versionComponents[1];
245 }
246 }
247
248 return new AccuRevVersion( basisStream, tran );
249 }
250
251 public String getSnapshotName( String tagName )
252 {
253 return tagToStream( tagName );
254 }
255
256 public String getRevision( String streamName, Date date )
257 {
258 return getRevision( streamName, AccuRev.ACCUREV_TIME_SPEC.format( date == null ? new Date() : date ) );
259 }
260
261 public String getRevision( String stream, long fromTranId )
262 {
263 return getRevision( stream, Long.toString( fromTranId ) );
264 }
265
266 public String getRevision( String streamName, String transaction )
267 {
268 return streamToTag( streamName ) + "/" + transaction;
269 }
270
271 public String getWorkSpaceRevision( String workspace )
272 throws AccuRevException
273 {
274 return getRevision( workspace, Long.toString( getCurrentTransactionId( workspace ) ) );
275 }
276
277 public Transaction getDepotTransaction( String stream, String tranSpec )
278 throws AccuRevException
279 {
280
281 if ( tranSpec == null )
282 {
283 tranSpec = "now";
284 }
285
286 List<Transaction> transactions = getAccuRev().history( stream, tranSpec, null, 1, true, true );
287
288 if ( transactions == null || transactions.isEmpty() )
289 {
290 logger.warn( "Unable to find transaction for tranSpec=" + tranSpec );
291 return null;
292 }
293 else
294 {
295 return transactions.get( 0 );
296 }
297
298 }
299
300 public String getDepotTransactionId( String stream, String tranSpec )
301 throws AccuRevException
302 {
303 Transaction t = getDepotTransaction( stream, tranSpec );
304
305 return t == null ? tranSpec : Long.toString( t.getTranId() );
306 }
307
308 private long getCurrentTransactionId( String workSpaceName )
309 throws AccuRevException
310 {
311
312
313
314 AccuRev accuRev = getAccuRev();
315
316 Map<String, WorkSpace> workSpaces = accuRev.showWorkSpaces();
317
318 WorkSpace workspace = workSpaces.get( workSpaceName );
319
320 if ( workspace == null )
321 {
322
323 workSpaces = accuRev.showRefTrees();
324 workspace = workSpaces.get( workSpaceName );
325 }
326
327 if ( workspace == null )
328 {
329 throw new AccuRevException( "Can't find workspace " + workSpaceName );
330 }
331 return workspace.getTransactionId();
332 }
333
334 public String toString()
335 {
336 StringBuilder buff = new StringBuilder( "AccuRevScmProviderRepository" );
337 buff.append( " user=" );
338 buff.append( getUser() );
339 buff.append( " pass=" );
340 buff.append( getPassword() == null ? "null" : StringUtils.repeat( "*", getPassword().length() ) );
341 buff.append( " host=" );
342 buff.append( getHost() );
343 buff.append( " port=" );
344 buff.append( getPort() );
345 buff.append( " stream=" );
346 buff.append( getStreamName() );
347 buff.append( " projectPath=" );
348 buff.append( getProjectPath() );
349
350 return buff.toString();
351 }
352
353 public static String formatTimeSpec( Date when )
354 {
355
356 if ( when == null )
357 {
358 return "now";
359 }
360
361 return AccuRev.ACCUREV_TIME_SPEC.format( when );
362
363 }
364
365 }