Mercurial > hg > mercurial
annotate mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 79:2e57d4e3f7b7
support checkout rules for server side patch
author | Pavel.Sher |
---|---|
date | Fri, 09 Oct 2009 21:42:55 +0400 |
parents | 4c15e7de1f9d |
children | 948a35b430e1 |
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 "); | |
71 | 242 final String obfuscatedUrl = CommandUtil.removePrivateData(settings.getRepositoryUrl(), Collections.singleton(settings.getPassword())); |
243 res.append(quoteIfNeeded(obfuscatedUrl)); | |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
244 res.append('\n').append(id.execute()); |
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
245 return res.toString(); |
0 | 246 } |
247 | |
17 | 248 private String quoteIfNeeded(@NotNull String str) { |
249 if (str.indexOf(' ') != -1) { | |
250 return "\"" + str + "\""; | |
251 } | |
252 | |
253 return str; | |
254 } | |
255 | |
0 | 256 @Nullable |
257 public Map<String, String> getDefaultVcsProperties() { | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
258 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
|
259 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
|
260 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
|
261 return defaults; |
0 | 262 } |
263 | |
264 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
|
265 return new ChangeSet(version).getId(); |
0 | 266 } |
267 | |
268 @NotNull | |
269 public Comparator<String> getVersionComparator() { | |
19 | 270 // comparator is called when TeamCity needs to sort modifications in the order of their appearance, |
271 // currently we sort changes by revision number, not sure however that this is a good idea, | |
272 // probably it would be better to sort them by timestamp (and to add timestamp into the version). | |
0 | 273 return new Comparator<String>() { |
274 public int compare(final String o1, final String o2) { | |
275 try { | |
276 return new ChangeSet(o1).getRevNumber() - new ChangeSet(o2).getRevNumber(); | |
277 } catch (Exception e) { | |
278 return 1; | |
279 } | |
280 } | |
281 }; | |
282 } | |
283 | |
284 public void buildPatch(final VcsRoot root, | |
285 @Nullable final String fromVersion, | |
286 @NotNull final String toVersion, | |
287 final PatchBuilder builder, | |
288 final CheckoutRules checkoutRules) throws IOException, VcsException { | |
44 | 289 syncClonedRepository(root); |
290 Settings settings = createSettings(root); | |
0 | 291 if (fromVersion == null) { |
79 | 292 buildFullPatch(settings, new ChangeSet(toVersion), builder, checkoutRules); |
0 | 293 } else { |
79 | 294 buildIncrementalPatch(settings, new ChangeSet(fromVersion), new ChangeSet(toVersion), builder, checkoutRules); |
0 | 295 } |
296 } | |
297 | |
19 | 298 // builds patch from version to version |
79 | 299 private void buildIncrementalPatch(final Settings settings, @NotNull final ChangeSet fromVer, @NotNull final ChangeSet toVer, final PatchBuilder builder, final CheckoutRules checkoutRules) |
0 | 300 throws VcsException, IOException { |
301 StatusCommand st = new StatusCommand(settings); | |
302 st.setFromRevId(fromVer.getId()); | |
303 st.setToRevId(toVer.getId()); | |
304 List<ModifiedFile> modifiedFiles = st.execute(); | |
305 List<String> notDeletedFiles = new ArrayList<String>(); | |
306 for (ModifiedFile f: modifiedFiles) { | |
307 if (f.getStatus() != ModifiedFile.Status.REMOVED) { | |
308 notDeletedFiles.add(f.getPath()); | |
309 } | |
310 } | |
311 | |
47
c785bc4c5f39
minor fix: do not call cat command if there are no files to export
Pavel.Sher
parents:
46
diff
changeset
|
312 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
|
313 |
0 | 314 CatCommand cc = new CatCommand(settings); |
315 cc.setRevId(toVer.getId()); | |
316 File parentDir = cc.execute(notDeletedFiles); | |
317 | |
318 try { | |
319 for (ModifiedFile f: modifiedFiles) { | |
79 | 320 String mappedPath = checkoutRules.map(f.getPath()); |
321 if (mappedPath == null) continue; // skip | |
322 final File virtualFile = new File(mappedPath); | |
0 | 323 if (f.getStatus() == ModifiedFile.Status.REMOVED) { |
324 builder.deleteFile(virtualFile, true); | |
325 } else { | |
326 File realFile = new File(parentDir, f.getPath()); | |
327 FileInputStream is = new FileInputStream(realFile); | |
328 try { | |
11 | 329 builder.changeOrCreateBinaryFile(virtualFile, null, is, realFile.length()); |
0 | 330 } finally { |
331 is.close(); | |
332 } | |
333 } | |
334 } | |
335 } finally { | |
336 FileUtil.delete(parentDir); | |
337 } | |
338 } | |
339 | |
19 | 340 // builds patch by exporting files using specified version |
79 | 341 private void buildFullPatch(final Settings settings, @NotNull final ChangeSet toVer, final PatchBuilder builder, final CheckoutRules checkoutRules) |
0 | 342 throws IOException, VcsException { |
343 CloneCommand cl = new CloneCommand(settings); | |
57 | 344 // clone from the local repository |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
345 cl.setRepository(settings.getLocalRepositoryDir().getAbsolutePath()); |
0 | 346 cl.setToId(toVer.getId()); |
57 | 347 cl.setUpdateWorkingDir(false); |
0 | 348 File tempDir = FileUtil.createTempDirectory("mercurial", toVer.getId()); |
349 try { | |
350 final File repRoot = new File(tempDir, "rep"); | |
351 cl.setDestDir(repRoot.getAbsolutePath()); | |
352 cl.execute(); | |
57 | 353 |
354 UpdateCommand up = new UpdateCommand(settings); | |
355 up.setWorkDirectory(repRoot.getAbsolutePath()); | |
356 up.setToId(toVer.getId()); | |
357 up.execute(); | |
358 | |
0 | 359 buildPatchFromDirectory(builder, repRoot, new FileFilter() { |
360 public boolean accept(final File file) { | |
361 return !(file.isDirectory() && ".hg".equals(file.getName())); | |
362 } | |
79 | 363 }, checkoutRules); |
0 | 364 } finally { |
365 FileUtil.delete(tempDir); | |
366 } | |
367 } | |
368 | |
79 | 369 private void buildPatchFromDirectory(final PatchBuilder builder, final File repRoot, final FileFilter filter, final CheckoutRules checkoutRules) throws IOException { |
370 buildPatchFromDirectory(repRoot, builder, repRoot, filter, checkoutRules); | |
0 | 371 } |
372 | |
79 | 373 private void buildPatchFromDirectory(File curDir, final PatchBuilder builder, final File repRoot, final FileFilter filter, final CheckoutRules checkoutRules) throws IOException { |
0 | 374 File[] files = curDir.listFiles(filter); |
375 if (files != null) { | |
376 for (File realFile: files) { | |
377 String relPath = realFile.getAbsolutePath().substring(repRoot.getAbsolutePath().length()); | |
79 | 378 String mappedPath = checkoutRules.map(relPath); |
379 if (mappedPath != null) { | |
380 final File virtualFile = new File(mappedPath); | |
381 if (realFile.isDirectory()) { | |
382 builder.createDirectory(virtualFile); | |
383 buildPatchFromDirectory(realFile, builder, repRoot, filter, checkoutRules); | |
384 } else { | |
385 final FileInputStream is = new FileInputStream(realFile); | |
386 try { | |
387 builder.createBinaryFile(virtualFile, null, is, realFile.length()); | |
388 } finally { | |
389 is.close(); | |
390 } | |
0 | 391 } |
392 } | |
393 } | |
394 } | |
395 } | |
396 | |
19 | 397 // updates current working copy of repository by pulling changes from the repository specified in VCS root |
44 | 398 private void syncClonedRepository(final VcsRoot root) throws VcsException { |
399 Settings settings = createSettings(root); | |
57 | 400 File workDir = settings.getLocalRepositoryDir(); |
21 | 401 lockWorkDir(workDir); |
402 try { | |
29 | 403 if (settings.hasCopyOfRepository()) { |
0 | 404 // update |
8 | 405 PullCommand pull = new PullCommand(settings); |
406 pull.execute(); | |
0 | 407 } else { |
408 // clone | |
409 CloneCommand cl = new CloneCommand(settings); | |
22 | 410 cl.setDestDir(workDir.getAbsolutePath()); |
18 | 411 cl.setUpdateWorkingDir(false); |
0 | 412 cl.execute(); |
413 } | |
21 | 414 } finally { |
415 unlockWorkDir(workDir); | |
0 | 416 } |
417 } | |
418 | |
44 | 419 @Override |
420 public LabelingSupport getLabelingSupport() { | |
421 return this; | |
422 } | |
423 | |
22 | 424 private void lockWorkDir(@NotNull File workDir) { |
21 | 425 getWorkDirLock(workDir).lock(); |
426 } | |
427 | |
22 | 428 private void unlockWorkDir(@NotNull File workDir) { |
21 | 429 getWorkDirLock(workDir).unlock(); |
430 } | |
431 | |
31 | 432 @Override |
433 public boolean ignoreServerCachesFor(final VcsRoot root) { | |
434 // since a copy of repository for each VCS root is already stored on disk | |
435 // we do not need separate cache for our patches | |
436 return true; | |
437 } | |
438 | |
22 | 439 private Lock getWorkDirLock(final File workDir) { |
440 String path = workDir.getAbsolutePath(); | |
441 Lock lock = myWorkDirLocks.get(path); | |
21 | 442 if (lock == null) { |
443 lock = new ReentrantLock(); | |
22 | 444 Lock curLock = myWorkDirLocks.putIfAbsent(path, lock); |
21 | 445 if (curLock != null) { |
446 lock = curLock; | |
447 } | |
448 } | |
449 return lock; | |
450 } | |
451 | |
23 | 452 private void removeOldWorkFolders() { |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
453 File workFoldersParent = myDefaultWorkFolderParent; |
23 | 454 if (!workFoldersParent.isDirectory()) return; |
455 | |
456 Set<File> workDirs = new HashSet<File>(); | |
457 File[] files = workFoldersParent.listFiles(new FileFilter() { | |
458 public boolean accept(final File file) { | |
459 return file.isDirectory() && file.getName().startsWith(Settings.DEFAULT_WORK_DIR_PREFIX); | |
460 } | |
461 }); | |
462 if (files != null) { | |
463 for (File f: files) { | |
46 | 464 workDirs.add(PathUtil.getCanonicalFile(f)); |
23 | 465 } |
466 } | |
467 | |
468 for (VcsRoot vcsRoot: myVcsManager.getAllRegisteredVcsRoots()) { | |
469 if (getName().equals(vcsRoot.getVcsName())) { | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
470 try { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
471 Settings s = createSettings(vcsRoot); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
472 workDirs.remove(PathUtil.getCanonicalFile(s.getLocalRepositoryDir())); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
473 } catch (VcsException e) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
474 Loggers.VCS.error(e); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
475 } |
23 | 476 } |
477 } | |
478 | |
479 for (File f: workDirs) { | |
480 lockWorkDir(f); | |
481 try { | |
482 FileUtil.delete(f); | |
483 } finally { | |
484 unlockWorkDir(f); | |
485 } | |
486 } | |
487 } | |
44 | 488 |
489 public String label(@NotNull String label, @NotNull String version, @NotNull VcsRoot root, @NotNull CheckoutRules checkoutRules) throws VcsException { | |
490 syncClonedRepository(root); | |
491 | |
492 Settings settings = createSettings(root); | |
493 | |
494 // I do not know why but hg tag does not work correctly if | |
495 // update command was not invoked for the current repo | |
496 // in such case if there were no tags before Mercurial attempts to | |
497 // create new head when tag is pushed to the parent repository | |
498 UpdateCommand uc = new UpdateCommand(settings); | |
499 uc.execute(); | |
500 | |
501 String fixedTagname = fixTagName(label); | |
502 TagCommand tc = new TagCommand(settings); | |
503 tc.setRevId(new ChangeSet(version).getId()); | |
504 tc.setTag(fixedTagname); | |
505 tc.execute(); | |
506 | |
507 PushCommand pc = new PushCommand(settings); | |
57 | 508 pc.setForce(true); |
44 | 509 pc.execute(); |
510 return fixedTagname; | |
511 } | |
512 | |
513 private String fixTagName(final String label) { | |
514 // according to Mercurial documentation http://hgbook.red-bean.com/hgbookch8.html#x12-1570008 | |
515 // tag name must not contain: | |
516 // Colon (ASCII 58, “:”) | |
517 // Carriage return (ASCII 13, “\r”) | |
518 // Newline (ASCII 10, “\n”) | |
519 // all these characters will be replaced with _ (underscore) | |
520 return label.replace(':', '_').replace('\r', '_').replace('\n', '_'); | |
521 } | |
522 | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
523 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
|
524 Settings settings = new Settings(myDefaultWorkFolderParent, root); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
525 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
|
526 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
|
527 File parentDir = new File(customClonePath); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
528 createClonedRepositoryParentDir(parentDir); |
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 // take last part of repository path |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
531 String repPath = settings.getRepositoryUrl(); |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
532 String[] splitted = repPath.split("[/\\\\]"); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
533 if (splitted.length > 0) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
534 repPath = splitted[splitted.length-1]; |
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 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
537 File customWorkingDir = new File(parentDir, repPath); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
538 settings.setWorkingDir(customWorkingDir); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
539 } else { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
540 createClonedRepositoryParentDir(myDefaultWorkFolderParent); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
541 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
542 return settings; |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
543 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
544 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
545 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
|
546 if (!parentDir.exists() && !parentDir.mkdirs()) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
547 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
|
548 } |
44 | 549 } |
48 | 550 |
551 public boolean isAgentSideCheckoutAvailable() { | |
552 return true; | |
553 } | |
0 | 554 } |