1 package org.apache.maven.scm.provider.svn;
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.ScmBranch;
23 import org.apache.maven.scm.ScmTag;
24 import org.apache.maven.scm.ScmVersion;
25 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
26 import org.codehaus.plexus.util.StringUtils;
27
28
29
30
31 public final class SvnTagBranchUtils
32 {
33
34 private SvnTagBranchUtils()
35 {
36 }
37
38 public static final String[] REVISION_SPECIFIERS = new String[]{"HEAD", "BASE", "COMMITTED", "PREV"};
39
40 public static final String SVN_TRUNK = "trunk";
41
42 public static final String SVN_BRANCHES = "branches";
43
44 public static final String SVN_TAGS = "tags";
45
46 public static final String[] SVN_BASE_DIRS = new String[]{SVN_TRUNK, SVN_BRANCHES, SVN_TAGS};
47
48
49
50
51
52
53 static String appendPath( String basePath, String addlPath )
54 {
55 basePath = StringUtils.stripEnd( basePath, "/" );
56
57 if ( StringUtils.isEmpty( addlPath ) )
58 {
59 return basePath;
60 }
61 else
62 {
63 return basePath + "/" + StringUtils.stripStart( addlPath, "/" );
64 }
65 }
66
67
68
69
70
71
72
73
74
75 public static String getProjectRoot( String repoPath )
76 {
77 for ( int i = 0; i < SVN_BASE_DIRS.length; i++ )
78 {
79 String base = "/" + SVN_BASE_DIRS[i];
80 int pos = repoPath.lastIndexOf( base + "/" );
81 if ( repoPath.endsWith( base ) )
82 {
83 return repoPath.substring( 0, repoPath.length() - base.length() );
84 }
85 else if ( pos >= 0 )
86 {
87 return repoPath.substring( 0, pos );
88 }
89 }
90
91
92
93 return appendPath( repoPath, null );
94 }
95
96 public static String resolveTagBase( SvnScmProviderRepository repository )
97 {
98 return resolveTagBase( repository.getUrl() );
99 }
100
101 public static String resolveTagBase( String repositoryUrl )
102 {
103 return appendPath( getProjectRoot( repositoryUrl ), SVN_TAGS );
104 }
105
106 public static String resolveBranchBase( SvnScmProviderRepository repository )
107 {
108 return resolveBranchBase( repository.getUrl() );
109 }
110
111 public static String resolveBranchBase( String repositoryUrl )
112 {
113 return appendPath( getProjectRoot( repositoryUrl ), SVN_BRANCHES );
114 }
115
116
117
118
119
120
121
122
123
124
125
126 public static String resolveTagUrl( SvnScmProviderRepository repository, ScmTag tag )
127 {
128 return resolveUrl( repository.getUrl(), repository.getTagBase(), SVN_TAGS, tag );
129 }
130
131
132
133
134
135
136
137
138
139
140 public static String resolveTagUrl( String repositoryUrl, ScmTag tag )
141 {
142 return resolveUrl( repositoryUrl, null, SVN_TAGS, tag );
143 }
144
145
146
147
148
149
150
151
152
153
154
155 public static String resolveBranchUrl( SvnScmProviderRepository repository, ScmBranch branch )
156 {
157 return resolveUrl( repository.getUrl(), repository.getBranchBase(), SVN_BRANCHES, branch );
158 }
159
160
161
162
163
164
165
166
167
168
169 public static String resolveBranchUrl( String repositoryUrl, ScmBranch branch )
170 {
171 return resolveUrl( repositoryUrl, resolveBranchBase( repositoryUrl ), SVN_BRANCHES, branch );
172 }
173
174 private static String addSuffix( String baseString, String suffix )
175 {
176 return ( suffix != null ) ? baseString + suffix : baseString;
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public static String resolveUrl( String repositoryUrl, String tagBase, String subdir, ScmBranch branchTag )
202 {
203 String branchTagName = branchTag.getName();
204 String projectRoot = getProjectRoot( repositoryUrl );
205 branchTagName = StringUtils.strip( branchTagName, "/" );
206
207 if ( StringUtils.isEmpty( branchTagName ) )
208 {
209 return null;
210 }
211
212
213 String queryString = null;
214 if ( repositoryUrl.indexOf( '?' ) >= 0 )
215 {
216 queryString = repositoryUrl.substring( repositoryUrl.indexOf( '?' ) );
217
218 projectRoot = StringUtils.replace( projectRoot, queryString, "" );
219 }
220
221 if ( branchTagName.indexOf( "://" ) >= 0 )
222 {
223
224 return branchTagName;
225 }
226
227
228 if ( StringUtils.isNotEmpty( tagBase ) && !tagBase.equals( resolveTagBase( repositoryUrl ) )
229 && !tagBase.equals( resolveBranchBase( repositoryUrl ) ) )
230 {
231 return appendPath( tagBase, branchTagName );
232 }
233
234
235
236 for ( int i = 0; i < SVN_BASE_DIRS.length; i++ )
237 {
238 if ( branchTagName.startsWith( SVN_BASE_DIRS[i] + "/" ) )
239 {
240 return addSuffix( appendPath( projectRoot, branchTagName ), queryString );
241 }
242 }
243
244 return addSuffix( appendPath( appendPath( projectRoot, subdir ), branchTagName ), queryString );
245 }
246
247
248
249 private static boolean checkRevisionArg( String arg )
250 {
251 if ( StringUtils.isNumeric( arg ) || ( arg.startsWith( "{" ) && arg.endsWith( "}" ) ) )
252 {
253 return true;
254 }
255
256 for ( int i = 0; i < REVISION_SPECIFIERS.length; i++ )
257 {
258 if ( REVISION_SPECIFIERS[i].equalsIgnoreCase( arg ) )
259 {
260 return true;
261 }
262 }
263
264 return false;
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 public static boolean isRevisionSpecifier( ScmVersion version )
282 {
283 if ( version == null )
284 {
285 return false;
286 }
287
288 String versionName = version.getName();
289
290 if ( StringUtils.isEmpty( versionName ) )
291 {
292 return false;
293 }
294
295 if ( checkRevisionArg( versionName ) )
296 {
297 return true;
298 }
299
300 String[] parts = StringUtils.split( versionName, ":" );
301 if ( parts.length == 2 && StringUtils.isNotEmpty( parts[0] ) && StringUtils.isNotEmpty( parts[1] ) )
302 {
303 return checkRevisionArg( parts[0] ) && checkRevisionArg( parts[1] );
304 }
305
306 return false;
307 }
308 }