1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
47
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
61
62
63
64
65
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
79
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
101
102
103
104
105
106
107
108
109 public static String resolveTagUrl(SvnScmProviderRepository repository, ScmTag tag) {
110 return resolveUrl(repository.getUrl(), repository.getTagBase(), SVN_TAGS, tag);
111 }
112
113
114
115
116
117
118
119
120
121
122 public static String resolveTagUrl(String repositoryUrl, ScmTag tag) {
123 return resolveUrl(repositoryUrl, null, SVN_TAGS, tag);
124 }
125
126
127
128
129
130
131
132
133
134
135
136 public static String resolveBranchUrl(SvnScmProviderRepository repository, ScmBranch branch) {
137 return resolveUrl(repository.getUrl(), repository.getBranchBase(), SVN_BRANCHES, branch);
138 }
139
140
141
142
143
144
145
146
147
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
188 String queryString = null;
189 if (repositoryUrl.indexOf('?') >= 0) {
190 queryString = repositoryUrl.substring(repositoryUrl.indexOf('?'));
191
192 projectRoot = StringUtils.replace(projectRoot, queryString, "");
193 }
194
195 if (branchTagName.indexOf("://") >= 0) {
196
197 return branchTagName;
198 }
199
200
201 if ((tagBase != null && !tagBase.isEmpty())
202 && !tagBase.equals(resolveTagBase(repositoryUrl))
203 && !tagBase.equals(resolveBranchBase(repositoryUrl))) {
204 return appendPath(tagBase, branchTagName);
205 }
206
207
208
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
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
236
237
238
239
240
241
242
243
244
245
246
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 }