1 package org.apache.maven.scm.provider.svn;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * Simple helper function to concatenate two paths together with a "/".
50 * Handles trailing / on basePath.
51 * Returns no trailing "/" if the addlPath is null
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 * Returns the project root for the given repository url,
69 * where "project root" is the root of the /trunk, /branches, /tags
70 * directories
71 *
72 * @param repoPath Repository path/url to be searched
73 * @return
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 // At this point we were unable to locate the project root of this url
92 // so assume that the repository url specified is the project root
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 * Resolves a tag to a repository url.
118 * By supplying the repository to this function (rather than calling {@link #resolveTagUrl(String,ScmTag)}
119 * the resolution can use the repository's tagBase to override the default tag location.
120 *
121 * @param repository the repository to use as a base for tag resolution
122 * @param tag tag name
123 * @return
124 * @see #resolveUrl(String,String,String,ScmBranch)
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 * Resolves a tag to a repository url.
133 * Will not use the {@link SvnScmProviderRepository#getTagBase()} during resolution.
134 *
135 * @param repositoryUrl string url for the repository
136 * @param tag tag name
137 * @return
138 * @see #resolveUrl(String,String,String,ScmBranch)
139 */
140 public static String resolveTagUrl( String repositoryUrl, ScmTag tag )
141 {
142 return resolveUrl( repositoryUrl, null, SVN_TAGS, tag );
143 }
144
145 /**
146 * Resolves a branch name to a repository url.
147 * By supplying the repository to this function (rather than calling {@link #resolveBranchUrl(String,ScmBranch)}
148 * the resolution can use the repository's tagBase to override the default tag location.
149 *
150 * @param repository the repository to use as a base for tag resolution
151 * @param branch tag name
152 * @return
153 * @see #resolveUrl(String,String,String,ScmBranch)
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 * Resolves a branch name to a repository url.
162 * Will not use the {@link SvnScmProviderRepository#getTagBase()} during resolution.
163 *
164 * @param repositoryUrl string url for the repository
165 * @param branch branch name
166 * @return
167 * @see #resolveUrl(String,String,String,ScmBranch)
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 * Resolves a tag or branch name to a repository url.<br>
182 * If the <code>branchTagName</code> is an absolute URL, that value is returned.
183 * (i.e. http://foo.com/svn/myproject/tags/my-tag)<br>
184 * <p/>
185 * If the repository has a {@link SvnScmProviderRepository#getTagBase()} specified,
186 * the tag is simply appended to the tagBase value. Note that at this time, we are using
187 * the tagBase as a base for both branches and tags.<br>
188 * <p/>
189 * If the <code>branchTagName</code> contains a branch/tag specifier (i.e. "/branches", "/tags", "/trunk"),
190 * the <code>branchTagName</code> is appended to the <code>projectRoot</code> without adding the subdir.<br>
191 * Else, the result is in the format of <code>projectRoot/subdir/branchTagName</code> directory.<br>
192 *
193 * @param repositoryUrl string url for the repository
194 * @param tagBase tagBase to use.
195 * @param subdir Subdirectory to append to the project root
196 * (for branching use "branches", tags use "tags")
197 * @param branchTag Name of the actual branch or tag. Can be an absolute url, simple tag/branch name,
198 * or even contain a relative path to the root like "branches/my-branch"
199 * @return
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 // Look for a query string as in ViewCVS urls
213 String queryString = null;
214 if ( repositoryUrl.indexOf( '?' ) >= 0 )
215 {
216 queryString = repositoryUrl.substring( repositoryUrl.indexOf( '?' ) );
217 // if repositoryUrl contains a query string, remove it from repositoryUrlRoot; will be re-appended later
218 projectRoot = StringUtils.replace( projectRoot, queryString, "" );
219 }
220
221 if ( branchTagName.indexOf( "://" ) >= 0 )
222 {
223 // branch/tag is already an absolute url so just return it.
224 return branchTagName;
225 }
226
227 // User has a tagBase specified so just return the name appended to the tagBase
228 if ( StringUtils.isNotEmpty( tagBase ) && !tagBase.equals( resolveTagBase( repositoryUrl ) )
229 && !tagBase.equals( resolveBranchBase( repositoryUrl ) ) )
230 {
231 return appendPath( tagBase, branchTagName );
232 }
233
234 // Look for any "branches/" or "tags/" specifiers in the branchTagName. If one occurs,
235 // don't append the subdir to the projectRoot when appending the name
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 /* Helper function that does the checking for {@link #isRevisionSpecifier}
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 * Returns whether the supplied tag refers to an actual revision or
269 * is specifying a tag/branch url in the repository.
270 * According to the subversion documentation, the following are valid revision specifiers:
271 * NUMBER revision number
272 * "{" DATE "}" revision at start of the date
273 * "HEAD" latest in repository
274 * "BASE" base rev of item's working copy
275 * "COMMITTED" last commit at or before BASE
276 * "PREV"
277 * <p/>
278 * For command such as diff, the revision argument can be in the format of:
279 * IDENTIFIER:IDENTIFIER where IDENTIFIER is one of the args listed above
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 }