Mercurial > hg > mercurial
annotate mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 67:e6971dc6b17c
always use url with credentials if username/password are specified
author | Pavel.Sher |
---|---|
date | Thu, 23 Jul 2009 11:42:23 +0400 |
parents | b328c6b6526d |
children | 4c15e7de1f9d |
rev | line source |
---|---|
25 | 1 /* |
2 * Copyright 2000-2007 JetBrains s.r.o. | |
3 * | |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 * you may not use this file except in compliance with the License. | |
6 * You may obtain a copy of the License at | |
7 * | |
8 * http://www.apache.org/licenses/LICENSE-2.0 | |
9 * | |
10 * Unless required by applicable law or agreed to in writing, software | |
11 * distributed under the License is distributed on an "AS IS" BASIS, | |
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 * See the License for the specific language governing permissions and | |
14 * limitations under the License. | |
15 */ | |
0 | 16 package jetbrains.buildServer.buildTriggers.vcs.mercurial; |
17 | |
48 | 18 import jetbrains.buildServer.AgentSideCheckoutAbility; |
0 | 19 import jetbrains.buildServer.CollectChangesByIncludeRule; |
20 import jetbrains.buildServer.Used; | |
21 import jetbrains.buildServer.buildTriggers.vcs.AbstractVcsPropertiesProcessor; | |
22 import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*; | |
23 import jetbrains.buildServer.log.Loggers; | |
24 import jetbrains.buildServer.serverSide.InvalidProperty; | |
25 import jetbrains.buildServer.serverSide.PropertiesProcessor; | |
21 | 26 import jetbrains.buildServer.serverSide.SBuildServer; |
0 | 27 import jetbrains.buildServer.serverSide.ServerPaths; |
28 import jetbrains.buildServer.util.FileUtil; | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
29 import jetbrains.buildServer.util.StringUtil; |
0 | 30 import jetbrains.buildServer.vcs.*; |
31 import jetbrains.buildServer.vcs.patches.PatchBuilder; | |
32 import org.jetbrains.annotations.NotNull; | |
33 import org.jetbrains.annotations.Nullable; | |
34 | |
1 | 35 import java.io.File; |
36 import java.io.FileFilter; | |
37 import java.io.FileInputStream; | |
38 import java.io.IOException; | |
39 import java.util.*; | |
21 | 40 import java.util.concurrent.ConcurrentHashMap; |
41 import java.util.concurrent.ConcurrentMap; | |
42 import java.util.concurrent.TimeUnit; | |
43 import java.util.concurrent.locks.Lock; | |
44 import java.util.concurrent.locks.ReentrantLock; | |
1 | 45 |
19 | 46 /** |
47 * Mercurial VCS plugin for TeamCity works as follows: | |
48 * <ul> | |
49 * <li>clones repository to internal storage | |
50 * <li>before any operation with working copy of repository pulls changes from the original repository | |
51 * <li>executes corresponding hg command | |
52 * </ul> | |
53 * | |
57 | 54 * <p>Working copy of repository is created in the $TEAMCITY_DATA_PATH/system/caches/hg_<hash code> folder. |
19 | 55 * <p>Personal builds (remote runs) are not yet supported, they require corresponding functionality from the IDE. |
56 */ | |
48 | 57 public class MercurialVcsSupport extends VcsSupport implements CollectChangesByIncludeRule, LabelingSupport, AgentSideCheckoutAbility { |
21 | 58 private ConcurrentMap<String, Lock> myWorkDirLocks= new ConcurrentHashMap<String, Lock>(); |
59 private static final int OLD_WORK_DIRS_CLEANUP_PERIOD = 600; | |
60 private VcsManager myVcsManager; | |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
61 private File myDefaultWorkFolderParent; |
0 | 62 |
21 | 63 public MercurialVcsSupport(@NotNull final VcsManager vcsManager, |
64 @NotNull ServerPaths paths, | |
65 @NotNull SBuildServer server) { | |
0 | 66 vcsManager.registerVcsSupport(this); |
21 | 67 myVcsManager = vcsManager; |
68 server.getExecutor().scheduleAtFixedRate(new Runnable() { | |
69 public void run() { | |
70 removeOldWorkFolders(); | |
71 } | |
72 }, 0, OLD_WORK_DIRS_CLEANUP_PERIOD, TimeUnit.SECONDS); | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
73 myDefaultWorkFolderParent = new File(paths.getCachesDir(), "mercurial"); |
0 | 74 } |
75 | |
76 public List<ModificationData> collectBuildChanges(final VcsRoot root, | |
77 @NotNull final String fromVersion, | |
78 @NotNull final String currentVersion, | |
79 final CheckoutRules checkoutRules) throws VcsException { | |
44 | 80 syncClonedRepository(root); |
0 | 81 return VcsSupportUtil.collectBuildChanges(root, fromVersion, currentVersion, checkoutRules, this); |
82 } | |
83 | |
84 public List<ModificationData> collectBuildChanges(final VcsRoot root, | |
85 final String fromVersion, | |
86 final String currentVersion, | |
87 final IncludeRule includeRule) throws VcsException { | |
19 | 88 // first obtain changes between specified versions |
0 | 89 List<ModificationData> result = new ArrayList<ModificationData>(); |
44 | 90 Settings settings = createSettings(root); |
0 | 91 LogCommand lc = new LogCommand(settings); |
57 | 92 String fromId = new ChangeSet(fromVersion).getId(); |
93 lc.setFromRevId(fromId); | |
0 | 94 lc.setToRevId(new ChangeSet(currentVersion).getId()); |
95 List<ChangeSet> changeSets = lc.execute(); | |
96 if (changeSets.isEmpty()) { | |
97 return result; | |
98 } | |
99 | |
19 | 100 // invoke status command for each changeset and determine what files were modified in these changesets |
0 | 101 StatusCommand st = new StatusCommand(settings); |
57 | 102 ChangeSet prev = new ChangeSet(fromVersion); |
103 for (int i=0; i<changeSets.size(); i++) { | |
104 ChangeSet cur = changeSets.get(i); | |
105 if (cur.getId().equals(fromId)) continue; // skip already reported changeset | |
106 | |
0 | 107 st.setFromRevId(prev.getId()); |
108 st.setToRevId(cur.getId()); | |
109 List<ModifiedFile> modifiedFiles = st.execute(); | |
19 | 110 // changeset full version will be set into VcsChange structure and |
20 | 111 // stored in database (note that getContent method will be invoked with this version) |
0 | 112 List<VcsChange> files = toVcsChanges(modifiedFiles, prev.getFullVersion(), cur.getFullVersion(), includeRule); |
113 if (files.isEmpty()) continue; | |
59
30cc10fe9479
accept version in two formats: with rev number and without
Pavel.Sher
parents:
58
diff
changeset
|
114 ModificationData md = new ModificationData(cur.getTimestamp(), files, cur.getSummary(), cur.getUser(), root, cur.getFullVersion(), cur.getId()); |
0 | 115 result.add(md); |
116 prev = cur; | |
117 } | |
118 | |
119 return result; | |
120 } | |
121 | |
122 private List<VcsChange> toVcsChanges(final List<ModifiedFile> modifiedFiles, String prevVer, String curVer, final IncludeRule includeRule) { | |
123 List<VcsChange> files = new ArrayList<VcsChange>(); | |
124 for (ModifiedFile mf: modifiedFiles) { | |
21 | 125 String normalizedPath = PathUtil.normalizeSeparator(mf.getPath()); |
12 | 126 if (!normalizedPath.startsWith(includeRule.getFrom())) continue; // skip files which do not match include rule |
0 | 127 |
128 VcsChangeInfo.Type changeType = getChangeType(mf.getStatus()); | |
129 if (changeType == null) { | |
130 Loggers.VCS.warn("Unable to convert status: " + mf.getStatus() + " to VCS change type"); | |
131 changeType = VcsChangeInfo.Type.NOT_CHANGED; | |
132 } | |
12 | 133 files.add(new VcsChange(changeType, mf.getStatus().getName(), normalizedPath, normalizedPath, prevVer, curVer)); |
0 | 134 } |
135 return files; | |
136 } | |
137 | |
138 private VcsChangeInfo.Type getChangeType(final ModifiedFile.Status status) { | |
139 switch (status) { | |
140 case ADDED:return VcsChangeInfo.Type.ADDED; | |
141 case MODIFIED:return VcsChangeInfo.Type.CHANGED; | |
142 case REMOVED:return VcsChangeInfo.Type.REMOVED; | |
143 } | |
144 return null; | |
145 } | |
146 | |
147 @NotNull | |
148 public byte[] getContent(final VcsModification vcsModification, | |
149 final VcsChangeInfo change, | |
150 final VcsChangeInfo.ContentType contentType, | |
151 final VcsRoot vcsRoot) throws VcsException { | |
44 | 152 syncClonedRepository(vcsRoot); |
12 | 153 String version = contentType == VcsChangeInfo.ContentType.AFTER_CHANGE ? change.getAfterChangeRevisionNumber() : change.getBeforeChangeRevisionNumber(); |
154 return getContent(change.getRelativeFileName(), vcsRoot, version); | |
0 | 155 } |
156 | |
157 @NotNull | |
12 | 158 public byte[] getContent(final String filePath, final VcsRoot vcsRoot, final String version) throws VcsException { |
44 | 159 syncClonedRepository(vcsRoot); |
160 Settings settings = createSettings(vcsRoot); | |
12 | 161 CatCommand cc = new CatCommand(settings); |
59
30cc10fe9479
accept version in two formats: with rev number and without
Pavel.Sher
parents:
58
diff
changeset
|
162 cc.setRevId(new ChangeSet(version).getId()); |
12 | 163 File parentDir = cc.execute(Collections.singletonList(filePath)); |
30 | 164 try { |
165 File file = new File(parentDir, filePath); | |
166 if (file.isFile()) { | |
167 try { | |
168 return FileUtil.loadFileBytes(file); | |
169 } catch (IOException e) { | |
170 throw new VcsException("Failed to load content of file: " + file.getAbsolutePath(), e); | |
171 } | |
172 } else { | |
173 Loggers.VCS.warn("Unable to obtain content of the file: " + filePath); | |
12 | 174 } |
30 | 175 } finally { |
176 FileUtil.delete(parentDir); | |
12 | 177 } |
0 | 178 return new byte[0]; |
179 } | |
180 | |
181 public String getName() { | |
28
a7cab5083ada
libraries moved on top level, dummy implementation of agent side checkout interface
Pavel.Sher
parents:
27
diff
changeset
|
182 return Constants.VCS_NAME; |
0 | 183 } |
184 | |
185 @Used("jsp") | |
186 public String getDisplayName() { | |
187 return "Mercurial"; | |
188 } | |
189 | |
190 @Nullable | |
191 public PropertiesProcessor getVcsPropertiesProcessor() { | |
192 return new AbstractVcsPropertiesProcessor() { | |
193 public Collection<InvalidProperty> process(final Map<String, String> properties) { | |
194 List<InvalidProperty> result = new ArrayList<InvalidProperty>(); | |
195 if (isEmpty(properties.get(Constants.HG_COMMAND_PATH_PROP))) { | |
196 result.add(new InvalidProperty(Constants.HG_COMMAND_PATH_PROP, "Path to 'hg' command must be specified")); | |
197 } | |
198 if (isEmpty(properties.get(Constants.REPOSITORY_PROP))) { | |
199 result.add(new InvalidProperty(Constants.REPOSITORY_PROP, "Repository must be specified")); | |
200 } | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
201 if (isEmpty(properties.get(Constants.SERVER_CLONE_PATH_PROP))) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
202 properties.put(Constants.SERVER_CLONE_PATH_PROP, myDefaultWorkFolderParent.getAbsolutePath()); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
203 } |
0 | 204 return result; |
205 } | |
206 }; | |
207 } | |
208 | |
209 public String getVcsSettingsJspFilePath() { | |
210 return "mercurialSettings.jsp"; | |
211 } | |
212 | |
213 @NotNull | |
214 public String getCurrentVersion(final VcsRoot root) throws VcsException { | |
19 | 215 // we will return full version of the most recent change as current version |
44 | 216 syncClonedRepository(root); |
217 Settings settings = createSettings(root); | |
57 | 218 BranchesCommand branches = new BranchesCommand(settings); |
219 Map<String, ChangeSet> result = branches.execute(); | |
220 if (!result.containsKey(settings.getBranchName())) { | |
221 throw new VcsException("Unable to find current version for the branch: " + settings.getBranchName()); | |
222 } | |
223 | |
224 return result.get(settings.getBranchName()).getFullVersion(); | |
0 | 225 } |
226 | |
227 public String describeVcsRoot(final VcsRoot vcsRoot) { | |
228 return "mercurial: " + vcsRoot.getProperty(Constants.REPOSITORY_PROP); | |
229 } | |
230 | |
231 public boolean isTestConnectionSupported() { | |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
232 return true; |
0 | 233 } |
234 | |
235 @Nullable | |
236 public String testConnection(final VcsRoot vcsRoot) throws VcsException { | |
44 | 237 Settings settings = createSettings(vcsRoot); |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
238 IdentifyCommand id = new IdentifyCommand(settings); |
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
239 StringBuilder res = new StringBuilder(); |
17 | 240 res.append(quoteIfNeeded(settings.getHgCommandPath())); |
241 res.append(" identify "); | |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
242 res.append(quoteIfNeeded(settings.getRepositoryUrl())); |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
243 res.append('\n').append(id.execute()); |
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
244 return res.toString(); |
0 | 245 } |
246 | |
17 | 247 private String quoteIfNeeded(@NotNull String str) { |
248 if (str.indexOf(' ') != -1) { | |
249 return "\"" + str + "\""; | |
250 } | |
251 | |
252 return str; | |
253 } | |
254 | |
0 | 255 @Nullable |
256 public Map<String, String> getDefaultVcsProperties() { | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
257 Map<String, String> defaults = new HashMap<String, String>(); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
258 defaults.put(Constants.HG_COMMAND_PATH_PROP, "hg"); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
259 defaults.put(Constants.SERVER_CLONE_PATH_PROP, myDefaultWorkFolderParent.getAbsolutePath()); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
260 return defaults; |
0 | 261 } |
262 | |
263 public String getVersionDisplayName(final String version, final VcsRoot root) throws VcsException { | |
59
30cc10fe9479
accept version in two formats: with rev number and without
Pavel.Sher
parents:
58
diff
changeset
|
264 return new ChangeSet(version).getId(); |
0 | 265 } |
266 | |
267 @NotNull | |
268 public Comparator<String> getVersionComparator() { | |
19 | 269 // comparator is called when TeamCity needs to sort modifications in the order of their appearance, |
270 // currently we sort changes by revision number, not sure however that this is a good idea, | |
271 // probably it would be better to sort them by timestamp (and to add timestamp into the version). | |
0 | 272 return new Comparator<String>() { |
273 public int compare(final String o1, final String o2) { | |
274 try { | |
275 return new ChangeSet(o1).getRevNumber() - new ChangeSet(o2).getRevNumber(); | |
276 } catch (Exception e) { | |
277 return 1; | |
278 } | |
279 } | |
280 }; | |
281 } | |
282 | |
283 public void buildPatch(final VcsRoot root, | |
284 @Nullable final String fromVersion, | |
285 @NotNull final String toVersion, | |
286 final PatchBuilder builder, | |
287 final CheckoutRules checkoutRules) throws IOException, VcsException { | |
44 | 288 syncClonedRepository(root); |
289 Settings settings = createSettings(root); | |
0 | 290 if (fromVersion == null) { |
291 buildFullPatch(settings, new ChangeSet(toVersion), builder); | |
292 } else { | |
293 buildIncrementalPatch(settings, new ChangeSet(fromVersion), new ChangeSet(toVersion), builder); | |
294 } | |
295 } | |
296 | |
19 | 297 // builds patch from version to version |
0 | 298 private void buildIncrementalPatch(final Settings settings, @NotNull final ChangeSet fromVer, @NotNull final ChangeSet toVer, final PatchBuilder builder) |
299 throws VcsException, IOException { | |
300 StatusCommand st = new StatusCommand(settings); | |
301 st.setFromRevId(fromVer.getId()); | |
302 st.setToRevId(toVer.getId()); | |
303 List<ModifiedFile> modifiedFiles = st.execute(); | |
304 List<String> notDeletedFiles = new ArrayList<String>(); | |
305 for (ModifiedFile f: modifiedFiles) { | |
306 if (f.getStatus() != ModifiedFile.Status.REMOVED) { | |
307 notDeletedFiles.add(f.getPath()); | |
308 } | |
309 } | |
310 | |
47
c785bc4c5f39
minor fix: do not call cat command if there are no files to export
Pavel.Sher
parents:
46
diff
changeset
|
311 if (notDeletedFiles.isEmpty()) return; |
c785bc4c5f39
minor fix: do not call cat command if there are no files to export
Pavel.Sher
parents:
46
diff
changeset
|
312 |
0 | 313 CatCommand cc = new CatCommand(settings); |
314 cc.setRevId(toVer.getId()); | |
315 File parentDir = cc.execute(notDeletedFiles); | |
316 | |
317 try { | |
318 for (ModifiedFile f: modifiedFiles) { | |
319 final File virtualFile = new File(f.getPath()); | |
320 if (f.getStatus() == ModifiedFile.Status.REMOVED) { | |
321 builder.deleteFile(virtualFile, true); | |
322 } else { | |
323 File realFile = new File(parentDir, f.getPath()); | |
324 FileInputStream is = new FileInputStream(realFile); | |
325 try { | |
11 | 326 builder.changeOrCreateBinaryFile(virtualFile, null, is, realFile.length()); |
0 | 327 } finally { |
328 is.close(); | |
329 } | |
330 } | |
331 } | |
332 } finally { | |
333 FileUtil.delete(parentDir); | |
334 } | |
335 } | |
336 | |
19 | 337 // builds patch by exporting files using specified version |
0 | 338 private void buildFullPatch(final Settings settings, @NotNull final ChangeSet toVer, final PatchBuilder builder) |
339 throws IOException, VcsException { | |
340 CloneCommand cl = new CloneCommand(settings); | |
57 | 341 // clone from the local repository |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
342 cl.setRepository(settings.getLocalRepositoryDir().getAbsolutePath()); |
0 | 343 cl.setToId(toVer.getId()); |
57 | 344 cl.setUpdateWorkingDir(false); |
0 | 345 File tempDir = FileUtil.createTempDirectory("mercurial", toVer.getId()); |
346 try { | |
347 final File repRoot = new File(tempDir, "rep"); | |
348 cl.setDestDir(repRoot.getAbsolutePath()); | |
349 cl.execute(); | |
57 | 350 |
351 UpdateCommand up = new UpdateCommand(settings); | |
352 up.setWorkDirectory(repRoot.getAbsolutePath()); | |
353 up.setToId(toVer.getId()); | |
354 up.execute(); | |
355 | |
0 | 356 buildPatchFromDirectory(builder, repRoot, new FileFilter() { |
357 public boolean accept(final File file) { | |
358 return !(file.isDirectory() && ".hg".equals(file.getName())); | |
359 } | |
360 }); | |
361 } finally { | |
362 FileUtil.delete(tempDir); | |
363 } | |
364 } | |
365 | |
366 private void buildPatchFromDirectory(final PatchBuilder builder, final File repRoot, final FileFilter filter) throws IOException { | |
367 buildPatchFromDirectory(repRoot, builder, repRoot, filter); | |
368 } | |
369 | |
370 private void buildPatchFromDirectory(File curDir, final PatchBuilder builder, final File repRoot, final FileFilter filter) throws IOException { | |
371 File[] files = curDir.listFiles(filter); | |
372 if (files != null) { | |
373 for (File realFile: files) { | |
374 String relPath = realFile.getAbsolutePath().substring(repRoot.getAbsolutePath().length()); | |
375 final File virtualFile = new File(relPath); | |
376 if (realFile.isDirectory()) { | |
377 builder.createDirectory(virtualFile); | |
378 buildPatchFromDirectory(realFile, builder, repRoot, filter); | |
379 } else { | |
380 final FileInputStream is = new FileInputStream(realFile); | |
381 try { | |
382 builder.createBinaryFile(virtualFile, null, is, realFile.length()); | |
383 } finally { | |
384 is.close(); | |
385 } | |
386 } | |
387 } | |
388 } | |
389 } | |
390 | |
19 | 391 // updates current working copy of repository by pulling changes from the repository specified in VCS root |
44 | 392 private void syncClonedRepository(final VcsRoot root) throws VcsException { |
393 Settings settings = createSettings(root); | |
57 | 394 File workDir = settings.getLocalRepositoryDir(); |
21 | 395 lockWorkDir(workDir); |
396 try { | |
29 | 397 if (settings.hasCopyOfRepository()) { |
0 | 398 // update |
8 | 399 PullCommand pull = new PullCommand(settings); |
400 pull.execute(); | |
0 | 401 } else { |
402 // clone | |
403 CloneCommand cl = new CloneCommand(settings); | |
22 | 404 cl.setDestDir(workDir.getAbsolutePath()); |
18 | 405 cl.setUpdateWorkingDir(false); |
0 | 406 cl.execute(); |
407 } | |
21 | 408 } finally { |
409 unlockWorkDir(workDir); | |
0 | 410 } |
411 } | |
412 | |
44 | 413 @Override |
414 public LabelingSupport getLabelingSupport() { | |
415 return this; | |
416 } | |
417 | |
22 | 418 private void lockWorkDir(@NotNull File workDir) { |
21 | 419 getWorkDirLock(workDir).lock(); |
420 } | |
421 | |
22 | 422 private void unlockWorkDir(@NotNull File workDir) { |
21 | 423 getWorkDirLock(workDir).unlock(); |
424 } | |
425 | |
31 | 426 @Override |
427 public boolean ignoreServerCachesFor(final VcsRoot root) { | |
428 // since a copy of repository for each VCS root is already stored on disk | |
429 // we do not need separate cache for our patches | |
430 return true; | |
431 } | |
432 | |
22 | 433 private Lock getWorkDirLock(final File workDir) { |
434 String path = workDir.getAbsolutePath(); | |
435 Lock lock = myWorkDirLocks.get(path); | |
21 | 436 if (lock == null) { |
437 lock = new ReentrantLock(); | |
22 | 438 Lock curLock = myWorkDirLocks.putIfAbsent(path, lock); |
21 | 439 if (curLock != null) { |
440 lock = curLock; | |
441 } | |
442 } | |
443 return lock; | |
444 } | |
445 | |
23 | 446 private void removeOldWorkFolders() { |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
447 File workFoldersParent = myDefaultWorkFolderParent; |
23 | 448 if (!workFoldersParent.isDirectory()) return; |
449 | |
450 Set<File> workDirs = new HashSet<File>(); | |
451 File[] files = workFoldersParent.listFiles(new FileFilter() { | |
452 public boolean accept(final File file) { | |
453 return file.isDirectory() && file.getName().startsWith(Settings.DEFAULT_WORK_DIR_PREFIX); | |
454 } | |
455 }); | |
456 if (files != null) { | |
457 for (File f: files) { | |
46 | 458 workDirs.add(PathUtil.getCanonicalFile(f)); |
23 | 459 } |
460 } | |
461 | |
462 for (VcsRoot vcsRoot: myVcsManager.getAllRegisteredVcsRoots()) { | |
463 if (getName().equals(vcsRoot.getVcsName())) { | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
464 try { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
465 Settings s = createSettings(vcsRoot); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
466 workDirs.remove(PathUtil.getCanonicalFile(s.getLocalRepositoryDir())); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
467 } catch (VcsException e) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
468 Loggers.VCS.error(e); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
469 } |
23 | 470 } |
471 } | |
472 | |
473 for (File f: workDirs) { | |
474 lockWorkDir(f); | |
475 try { | |
476 FileUtil.delete(f); | |
477 } finally { | |
478 unlockWorkDir(f); | |
479 } | |
480 } | |
481 } | |
44 | 482 |
483 public String label(@NotNull String label, @NotNull String version, @NotNull VcsRoot root, @NotNull CheckoutRules checkoutRules) throws VcsException { | |
484 syncClonedRepository(root); | |
485 | |
486 Settings settings = createSettings(root); | |
487 | |
488 // I do not know why but hg tag does not work correctly if | |
489 // update command was not invoked for the current repo | |
490 // in such case if there were no tags before Mercurial attempts to | |
491 // create new head when tag is pushed to the parent repository | |
492 UpdateCommand uc = new UpdateCommand(settings); | |
493 uc.execute(); | |
494 | |
495 String fixedTagname = fixTagName(label); | |
496 TagCommand tc = new TagCommand(settings); | |
497 tc.setRevId(new ChangeSet(version).getId()); | |
498 tc.setTag(fixedTagname); | |
499 tc.execute(); | |
500 | |
501 PushCommand pc = new PushCommand(settings); | |
57 | 502 pc.setForce(true); |
44 | 503 pc.execute(); |
504 return fixedTagname; | |
505 } | |
506 | |
507 private String fixTagName(final String label) { | |
508 // according to Mercurial documentation http://hgbook.red-bean.com/hgbookch8.html#x12-1570008 | |
509 // tag name must not contain: | |
510 // Colon (ASCII 58, “:”) | |
511 // Carriage return (ASCII 13, “\r”) | |
512 // Newline (ASCII 10, “\n”) | |
513 // all these characters will be replaced with _ (underscore) | |
514 return label.replace(':', '_').replace('\r', '_').replace('\n', '_'); | |
515 } | |
516 | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
517 private Settings createSettings(final VcsRoot root) throws VcsException { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
518 Settings settings = new Settings(myDefaultWorkFolderParent, root); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
519 String customClonePath = root.getProperty(Constants.SERVER_CLONE_PATH_PROP); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
520 if (!StringUtil.isEmptyOrSpaces(customClonePath) && !myDefaultWorkFolderParent.equals(new File(customClonePath).getAbsoluteFile())) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
521 File parentDir = new File(customClonePath); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
522 createClonedRepositoryParentDir(parentDir); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
523 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
524 // take last part of repository path |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
525 String repPath = settings.getRepositoryUrl(); |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
526 String[] splitted = repPath.split("[/\\\\]"); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
527 if (splitted.length > 0) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
528 repPath = splitted[splitted.length-1]; |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
529 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
530 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
531 File customWorkingDir = new File(parentDir, repPath); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
532 settings.setWorkingDir(customWorkingDir); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
533 } else { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
534 createClonedRepositoryParentDir(myDefaultWorkFolderParent); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
535 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
536 return settings; |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
537 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
538 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
539 private void createClonedRepositoryParentDir(final File parentDir) throws VcsException { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
540 if (!parentDir.exists() && !parentDir.mkdirs()) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
541 throw new VcsException("Failed to create parent directory for cloned repository: " + parentDir.getAbsolutePath()); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
542 } |
44 | 543 } |
48 | 544 |
545 public boolean isAgentSideCheckoutAvailable() { | |
546 return true; | |
547 } | |
0 | 548 } |