001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.svn; 020 021import org.apache.commons.lang3.StringUtils; 022import org.apache.maven.scm.ScmBranch; 023import org.apache.maven.scm.ScmTag; 024import org.apache.maven.scm.ScmVersion; 025import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository; 026 027/** 028 * 029 */ 030public final class SvnTagBranchUtils { 031 032 private SvnTagBranchUtils() {} 033 034 public static final String[] REVISION_SPECIFIERS = new String[] {"HEAD", "BASE", "COMMITTED", "PREV"}; 035 036 public static final String SVN_TRUNK = "trunk"; 037 038 public static final String SVN_BRANCHES = "branches"; 039 040 public static final String SVN_TAGS = "tags"; 041 042 public static final String[] SVN_BASE_DIRS = new String[] {SVN_TRUNK, SVN_BRANCHES, SVN_TAGS}; 043 044 /** 045 * Simple helper function to concatenate two paths together with a "/". 046 * Handles trailing / on basePath. 047 * Returns no trailing "/" if the addlPath is null 048 */ 049 static String appendPath(String basePath, String addlPath) { 050 basePath = StringUtils.stripEnd(basePath, "/"); 051 052 if (addlPath == null || addlPath.isEmpty()) { 053 return basePath; 054 } else { 055 return basePath + "/" + StringUtils.stripStart(addlPath, "/"); 056 } 057 } 058 059 /** 060 * Returns the project root for the given repository url, 061 * where "project root" is the root of the /trunk, /branches, /tags 062 * directories 063 * 064 * @param repoPath Repository path/url to be searched 065 * @return TODO 066 */ 067 public static String getProjectRoot(String repoPath) { 068 for (int i = 0; i < SVN_BASE_DIRS.length; i++) { 069 String base = "/" + SVN_BASE_DIRS[i]; 070 int pos = repoPath.lastIndexOf(base + "/"); 071 if (repoPath.endsWith(base)) { 072 return repoPath.substring(0, repoPath.length() - base.length()); 073 } else if (pos >= 0) { 074 return repoPath.substring(0, pos); 075 } 076 } 077 078 // At this point we were unable to locate the project root of this url 079 // so assume that the repository url specified is the project root 080 return appendPath(repoPath, null); 081 } 082 083 public static String resolveTagBase(SvnScmProviderRepository repository) { 084 return resolveTagBase(repository.getUrl()); 085 } 086 087 public static String resolveTagBase(String repositoryUrl) { 088 return appendPath(getProjectRoot(repositoryUrl), SVN_TAGS); 089 } 090 091 public static String resolveBranchBase(SvnScmProviderRepository repository) { 092 return resolveBranchBase(repository.getUrl()); 093 } 094 095 public static String resolveBranchBase(String repositoryUrl) { 096 return appendPath(getProjectRoot(repositoryUrl), SVN_BRANCHES); 097 } 098 099 /** 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}