View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn;
20  
21  import org.apache.commons.lang3.StringUtils;
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  
27  /**
28   *
29   */
30  public final class SvnTagBranchUtils {
31  
32      private SvnTagBranchUtils() {}
33  
34      public static final String[] REVISION_SPECIFIERS = new String[] {"HEAD", "BASE", "COMMITTED", "PREV"};
35  
36      public static final String SVN_TRUNK = "trunk";
37  
38      public static final String SVN_BRANCHES = "branches";
39  
40      public static final String SVN_TAGS = "tags";
41  
42      public static final String[] SVN_BASE_DIRS = new String[] {SVN_TRUNK, SVN_BRANCHES, SVN_TAGS};
43  
44      /**
45       * Simple helper function to concatenate two paths together with a "/".
46       * Handles trailing / on basePath.
47       * Returns no trailing "/" if the addlPath is null
48       */
49      static String appendPath(String basePath, String addlPath) {
50          basePath = StringUtils.stripEnd(basePath, "/");
51  
52          if (addlPath == null || addlPath.isEmpty()) {
53              return basePath;
54          } else {
55              return basePath + "/" + StringUtils.stripStart(addlPath, "/");
56          }
57      }
58  
59      /**
60       * Returns the project root for the given repository url,
61       * where "project root" is the root of the /trunk, /branches, /tags
62       * directories
63       *
64       * @param repoPath Repository path/url to be searched
65       * @return TODO
66       */
67      public static String getProjectRoot(String repoPath) {
68          for (int i = 0; i < SVN_BASE_DIRS.length; i++) {
69              String base = "/" + SVN_BASE_DIRS[i];
70              int pos = repoPath.lastIndexOf(base + "/");
71              if (repoPath.endsWith(base)) {
72                  return repoPath.substring(0, repoPath.length() - base.length());
73              } else if (pos >= 0) {
74                  return repoPath.substring(0, pos);
75              }
76          }
77  
78          // At this point we were unable to locate the project root of this url
79          // so assume that the repository url specified is the project root
80          return appendPath(repoPath, null);
81      }
82  
83      public static String resolveTagBase(SvnScmProviderRepository repository) {
84          return resolveTagBase(repository.getUrl());
85      }
86  
87      public static String resolveTagBase(String repositoryUrl) {
88          return appendPath(getProjectRoot(repositoryUrl), SVN_TAGS);
89      }
90  
91      public static String resolveBranchBase(SvnScmProviderRepository repository) {
92          return resolveBranchBase(repository.getUrl());
93      }
94  
95      public static String resolveBranchBase(String repositoryUrl) {
96          return appendPath(getProjectRoot(repositoryUrl), SVN_BRANCHES);
97      }
98  
99      /**
100      * Resolves a tag to a repository url.
101      * By supplying the repository to this function (rather than calling {@link #resolveTagUrl(String,ScmTag)}
102      * the resolution can use the repository's tagBase to override the default tag location.
103      *
104      * @param repository the repository to use as a base for tag resolution
105      * @param tag        tag name
106      * @return TODO
107      * @see #resolveUrl(String,String,String,ScmBranch)
108      */
109     public static String resolveTagUrl(SvnScmProviderRepository repository, ScmTag tag) {
110         return resolveUrl(repository.getUrl(), repository.getTagBase(), SVN_TAGS, tag);
111     }
112 
113     /**
114      * Resolves a tag to a repository url.
115      * Will not use the {@link SvnScmProviderRepository#getTagBase()} during resolution.
116      *
117      * @param repositoryUrl string url for the repository
118      * @param tag           tag name
119      * @return TODO
120      * @see #resolveUrl(String,String,String,ScmBranch)
121      */
122     public static String resolveTagUrl(String repositoryUrl, ScmTag tag) {
123         return resolveUrl(repositoryUrl, null, SVN_TAGS, tag);
124     }
125 
126     /**
127      * Resolves a branch name to a repository url.
128      * By supplying the repository to this function (rather than calling {@link #resolveBranchUrl(String,ScmBranch)}
129      * the resolution can use the repository's tagBase to override the default tag location.
130      *
131      * @param repository the repository to use as a base for tag resolution
132      * @param branch     tag name
133      * @return TODO
134      * @see #resolveUrl(String,String,String,ScmBranch)
135      */
136     public static String resolveBranchUrl(SvnScmProviderRepository repository, ScmBranch branch) {
137         return resolveUrl(repository.getUrl(), repository.getBranchBase(), SVN_BRANCHES, branch);
138     }
139 
140     /**
141      * Resolves a branch name to a repository url.
142      * Will not use the {@link SvnScmProviderRepository#getTagBase()} during resolution.
143      *
144      * @param repositoryUrl string url for the repository
145      * @param branch        branch name
146      * @return TODO
147      * @see #resolveUrl(String,String,String,ScmBranch)
148      */
149     public static String resolveBranchUrl(String repositoryUrl, ScmBranch branch) {
150         return resolveUrl(repositoryUrl, resolveBranchBase(repositoryUrl), SVN_BRANCHES, branch);
151     }
152 
153     private static String addSuffix(String baseString, String suffix) {
154         return (suffix != null) ? baseString + suffix : baseString;
155     }
156 
157     /**
158      * Resolves a tag or branch name to a repository url.<br>
159      * If the <code>branchTagName</code> is an absolute URL, that value is returned.
160      * (i.e. http://foo.com/svn/myproject/tags/my-tag)<br>
161      * <p>
162      * If the repository has a {@link SvnScmProviderRepository#getTagBase()} specified,
163      * the tag is simply appended to the tagBase value. Note that at this time, we are using
164      * the tagBase as a base for both branches and tags.<br>
165      * <p>
166      * If the <code>branchTagName</code> contains a branch/tag specifier (i.e. "/branches", "/tags", "/trunk"),
167      * the <code>branchTagName</code> is appended to the <code>projectRoot</code> without adding the subdir.<br>
168      * Else, the result is in the format of <code>projectRoot/subdir/branchTagName</code> directory.<br>
169      *
170      * @param repositoryUrl string url for the repository
171      * @param tagBase       tagBase to use.
172      * @param subdir        Subdirectory to append to the project root
173      *                      (for branching use "branches", tags use "tags")
174      * @param branchTag     Name of the actual branch or tag. Can be an absolute url, simple tag/branch name,
175      *                      or even contain a relative path to the root like "branches/my-branch"
176      * @return TODO
177      */
178     public static String resolveUrl(String repositoryUrl, String tagBase, String subdir, ScmBranch branchTag) {
179         String branchTagName = branchTag.getName();
180         String projectRoot = getProjectRoot(repositoryUrl);
181         branchTagName = StringUtils.strip(branchTagName, "/");
182 
183         if (branchTagName == null || branchTagName.isEmpty()) {
184             return null;
185         }
186 
187         // Look for a query string as in ViewSVN urls
188         String queryString = null;
189         if (repositoryUrl.indexOf('?') >= 0) {
190             queryString = repositoryUrl.substring(repositoryUrl.indexOf('?'));
191             // if repositoryUrl contains a query string, remove it from repositoryUrlRoot; will be re-appended later
192             projectRoot = StringUtils.replace(projectRoot, queryString, "");
193         }
194 
195         if (branchTagName.indexOf("://") >= 0) {
196             // branch/tag is already an absolute url so just return it.
197             return branchTagName;
198         }
199 
200         // User has a tagBase specified so just return the name appended to the tagBase
201         if ((tagBase != null && !tagBase.isEmpty())
202                 && !tagBase.equals(resolveTagBase(repositoryUrl))
203                 && !tagBase.equals(resolveBranchBase(repositoryUrl))) {
204             return appendPath(tagBase, branchTagName);
205         }
206 
207         // Look for any "branches/" or "tags/" specifiers in the branchTagName. If one occurs,
208         // don't append the subdir to the projectRoot when appending the name
209         for (int i = 0; i < SVN_BASE_DIRS.length; i++) {
210             if (branchTagName.startsWith(SVN_BASE_DIRS[i] + "/")) {
211                 return addSuffix(appendPath(projectRoot, branchTagName), queryString);
212             }
213         }
214 
215         return addSuffix(appendPath(appendPath(projectRoot, subdir), branchTagName), queryString);
216     }
217 
218     /* Helper function that does the checking for {@link #isRevisionSpecifier}
219      */
220     private static boolean checkRevisionArg(String arg) {
221         if (StringUtils.isNumeric(arg) || (arg.startsWith("{") && arg.endsWith("}"))) {
222             return true;
223         }
224 
225         for (int i = 0; i < REVISION_SPECIFIERS.length; i++) {
226             if (REVISION_SPECIFIERS[i].equalsIgnoreCase(arg)) {
227                 return true;
228             }
229         }
230 
231         return false;
232     }
233 
234     /**
235      * Returns whether the supplied tag refers to an actual revision or
236      * is specifying a tag/branch url in the repository.
237      * According to the subversion documentation, the following are valid revision specifiers:
238      * NUMBER       revision number
239      * "{" DATE "}" revision at start of the date
240      * "HEAD"       latest in repository
241      * "BASE"       base rev of item's working copy
242      * "COMMITTED"  last commit at or before BASE
243      * "PREV"
244      * <p>
245      * For command such as diff, the revision argument can be in the format of:
246      * IDENTIFIER:IDENTIFIER   where IDENTIFIER is one of the args listed above
247      */
248     public static boolean isRevisionSpecifier(ScmVersion version) {
249         if (version == null) {
250             return false;
251         }
252 
253         String versionName = version.getName();
254 
255         if (versionName == null || versionName.isEmpty()) {
256             return false;
257         }
258 
259         if (checkRevisionArg(versionName)) {
260             return true;
261         }
262 
263         String[] parts = StringUtils.split(versionName, ":");
264         if (parts.length == 2 && StringUtils.isNotEmpty(parts[0]) && StringUtils.isNotEmpty(parts[1])) {
265             return checkRevisionArg(parts[0]) && checkRevisionArg(parts[1]);
266         }
267 
268         return false;
269     }
270 }