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