Mercurial > hg > mercurial
annotate mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 86:948a35b430e1 Darjeeling-5.0.x
report parent changesets
author | Pavel.Sher |
---|---|
date | Tue, 08 Dec 2009 19:49:28 +0300 |
parents | 2e57d4e3f7b7 |
children | 5d9c34cb543a |
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); |
86 | 92 String fromId = new ChangeSetRevision(fromVersion).getId(); |
57 | 93 lc.setFromRevId(fromId); |
86 | 94 lc.setToRevId(new ChangeSetRevision(currentVersion).getId()); |
0 | 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); |
86 | 103 for (ChangeSet cur : changeSets) { |
57 | 104 if (cur.getId().equals(fromId)) continue; // skip already reported changeset |
105 | |
0 | 106 st.setFromRevId(prev.getId()); |
107 st.setToRevId(cur.getId()); | |
108 List<ModifiedFile> modifiedFiles = st.execute(); | |
19 | 109 // changeset full version will be set into VcsChange structure and |
20 | 110 // stored in database (note that getContent method will be invoked with this version) |
0 | 111 List<VcsChange> files = toVcsChanges(modifiedFiles, prev.getFullVersion(), cur.getFullVersion(), includeRule); |
112 if (files.isEmpty()) continue; | |
59
30cc10fe9479
accept version in two formats: with rev number and without
Pavel.Sher
parents:
58
diff
changeset
|
113 ModificationData md = new ModificationData(cur.getTimestamp(), files, cur.getSummary(), cur.getUser(), root, cur.getFullVersion(), cur.getId()); |
0 | 114 result.add(md); |
115 prev = cur; | |
116 } | |
117 | |
118 return result; | |
119 } | |
120 | |
121 private List<VcsChange> toVcsChanges(final List<ModifiedFile> modifiedFiles, String prevVer, String curVer, final IncludeRule includeRule) { | |
122 List<VcsChange> files = new ArrayList<VcsChange>(); | |
123 for (ModifiedFile mf: modifiedFiles) { | |
21 | 124 String normalizedPath = PathUtil.normalizeSeparator(mf.getPath()); |
12 | 125 if (!normalizedPath.startsWith(includeRule.getFrom())) continue; // skip files which do not match include rule |
0 | 126 |
127 VcsChangeInfo.Type changeType = getChangeType(mf.getStatus()); | |
128 if (changeType == null) { | |
129 Loggers.VCS.warn("Unable to convert status: " + mf.getStatus() + " to VCS change type"); | |
130 changeType = VcsChangeInfo.Type.NOT_CHANGED; | |
131 } | |
12 | 132 files.add(new VcsChange(changeType, mf.getStatus().getName(), normalizedPath, normalizedPath, prevVer, curVer)); |
0 | 133 } |
134 return files; | |
135 } | |
136 | |
137 private VcsChangeInfo.Type getChangeType(final ModifiedFile.Status status) { | |
138 switch (status) { | |
139 case ADDED:return VcsChangeInfo.Type.ADDED; | |
140 case MODIFIED:return VcsChangeInfo.Type.CHANGED; | |
141 case REMOVED:return VcsChangeInfo.Type.REMOVED; | |
142 } | |
143 return null; | |
144 } | |
145 | |
146 @NotNull | |
86 | 147 public byte[] getContent(@NotNull final VcsModification vcsModification, |
148 @NotNull final VcsChangeInfo change, | |
149 @NotNull final VcsChangeInfo.ContentType contentType, | |
150 @NotNull final VcsRoot vcsRoot) throws VcsException { | |
44 | 151 syncClonedRepository(vcsRoot); |
12 | 152 String version = contentType == VcsChangeInfo.ContentType.AFTER_CHANGE ? change.getAfterChangeRevisionNumber() : change.getBeforeChangeRevisionNumber(); |
153 return getContent(change.getRelativeFileName(), vcsRoot, version); | |
0 | 154 } |
155 | |
156 @NotNull | |
86 | 157 public byte[] getContent(@NotNull final String filePath, @NotNull final VcsRoot vcsRoot, @NotNull final String version) throws VcsException { |
44 | 158 syncClonedRepository(vcsRoot); |
159 Settings settings = createSettings(vcsRoot); | |
12 | 160 CatCommand cc = new CatCommand(settings); |
59
30cc10fe9479
accept version in two formats: with rev number and without
Pavel.Sher
parents:
58
diff
changeset
|
161 cc.setRevId(new ChangeSet(version).getId()); |
12 | 162 File parentDir = cc.execute(Collections.singletonList(filePath)); |
30 | 163 try { |
164 File file = new File(parentDir, filePath); | |
165 if (file.isFile()) { | |
166 try { | |
167 return FileUtil.loadFileBytes(file); | |
168 } catch (IOException e) { | |
169 throw new VcsException("Failed to load content of file: " + file.getAbsolutePath(), e); | |
170 } | |
171 } else { | |
172 Loggers.VCS.warn("Unable to obtain content of the file: " + filePath); | |
12 | 173 } |
30 | 174 } finally { |
175 FileUtil.delete(parentDir); | |
12 | 176 } |
0 | 177 return new byte[0]; |
178 } | |
179 | |
86 | 180 @NotNull |
0 | 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 | |
86 | 185 @NotNull |
0 | 186 @Used("jsp") |
187 public String getDisplayName() { | |
188 return "Mercurial"; | |
189 } | |
190 | |
191 @Nullable | |
192 public PropertiesProcessor getVcsPropertiesProcessor() { | |
193 return new AbstractVcsPropertiesProcessor() { | |
194 public Collection<InvalidProperty> process(final Map<String, String> properties) { | |
195 List<InvalidProperty> result = new ArrayList<InvalidProperty>(); | |
196 if (isEmpty(properties.get(Constants.HG_COMMAND_PATH_PROP))) { | |
197 result.add(new InvalidProperty(Constants.HG_COMMAND_PATH_PROP, "Path to 'hg' command must be specified")); | |
198 } | |
199 if (isEmpty(properties.get(Constants.REPOSITORY_PROP))) { | |
200 result.add(new InvalidProperty(Constants.REPOSITORY_PROP, "Repository must be specified")); | |
201 } | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
202 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
|
203 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
|
204 } |
0 | 205 return result; |
206 } | |
207 }; | |
208 } | |
209 | |
86 | 210 @NotNull |
0 | 211 public String getVcsSettingsJspFilePath() { |
212 return "mercurialSettings.jsp"; | |
213 } | |
214 | |
215 @NotNull | |
86 | 216 public String getCurrentVersion(@NotNull final VcsRoot root) throws VcsException { |
19 | 217 // we will return full version of the most recent change as current version |
44 | 218 syncClonedRepository(root); |
219 Settings settings = createSettings(root); | |
57 | 220 BranchesCommand branches = new BranchesCommand(settings); |
221 Map<String, ChangeSet> result = branches.execute(); | |
222 if (!result.containsKey(settings.getBranchName())) { | |
223 throw new VcsException("Unable to find current version for the branch: " + settings.getBranchName()); | |
224 } | |
225 | |
226 return result.get(settings.getBranchName()).getFullVersion(); | |
0 | 227 } |
228 | |
86 | 229 @NotNull |
0 | 230 public String describeVcsRoot(final VcsRoot vcsRoot) { |
231 return "mercurial: " + vcsRoot.getProperty(Constants.REPOSITORY_PROP); | |
232 } | |
233 | |
234 public boolean isTestConnectionSupported() { | |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
235 return true; |
0 | 236 } |
237 | |
238 @Nullable | |
86 | 239 public String testConnection(@NotNull final VcsRoot vcsRoot) throws VcsException { |
44 | 240 Settings settings = createSettings(vcsRoot); |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
241 IdentifyCommand id = new IdentifyCommand(settings); |
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
242 StringBuilder res = new StringBuilder(); |
17 | 243 res.append(quoteIfNeeded(settings.getHgCommandPath())); |
244 res.append(" identify "); | |
71 | 245 final String obfuscatedUrl = CommandUtil.removePrivateData(settings.getRepositoryUrl(), Collections.singleton(settings.getPassword())); |
246 res.append(quoteIfNeeded(obfuscatedUrl)); | |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
247 res.append('\n').append(id.execute()); |
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
248 return res.toString(); |
0 | 249 } |
250 | |
17 | 251 private String quoteIfNeeded(@NotNull String str) { |
252 if (str.indexOf(' ') != -1) { | |
253 return "\"" + str + "\""; | |
254 } | |
255 | |
256 return str; | |
257 } | |
258 | |
0 | 259 @Nullable |
260 public Map<String, String> getDefaultVcsProperties() { | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
261 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
|
262 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
|
263 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
|
264 return defaults; |
0 | 265 } |
266 | |
86 | 267 public String getVersionDisplayName(@NotNull final String version, @NotNull final VcsRoot root) throws VcsException { |
59
30cc10fe9479
accept version in two formats: with rev number and without
Pavel.Sher
parents:
58
diff
changeset
|
268 return new ChangeSet(version).getId(); |
0 | 269 } |
270 | |
271 @NotNull | |
272 public Comparator<String> getVersionComparator() { | |
19 | 273 // comparator is called when TeamCity needs to sort modifications in the order of their appearance, |
274 // currently we sort changes by revision number, not sure however that this is a good idea, | |
275 // probably it would be better to sort them by timestamp (and to add timestamp into the version). | |
0 | 276 return new Comparator<String>() { |
277 public int compare(final String o1, final String o2) { | |
278 try { | |
279 return new ChangeSet(o1).getRevNumber() - new ChangeSet(o2).getRevNumber(); | |
280 } catch (Exception e) { | |
281 return 1; | |
282 } | |
283 } | |
284 }; | |
285 } | |
286 | |
86 | 287 public void buildPatch(@NotNull final VcsRoot root, |
0 | 288 @Nullable final String fromVersion, |
289 @NotNull final String toVersion, | |
86 | 290 @NotNull final PatchBuilder builder, |
291 @NotNull final CheckoutRules checkoutRules) throws IOException, VcsException { | |
44 | 292 syncClonedRepository(root); |
293 Settings settings = createSettings(root); | |
0 | 294 if (fromVersion == null) { |
79 | 295 buildFullPatch(settings, new ChangeSet(toVersion), builder, checkoutRules); |
0 | 296 } else { |
79 | 297 buildIncrementalPatch(settings, new ChangeSet(fromVersion), new ChangeSet(toVersion), builder, checkoutRules); |
0 | 298 } |
299 } | |
300 | |
19 | 301 // builds patch from version to version |
79 | 302 private void buildIncrementalPatch(final Settings settings, @NotNull final ChangeSet fromVer, @NotNull final ChangeSet toVer, final PatchBuilder builder, final CheckoutRules checkoutRules) |
0 | 303 throws VcsException, IOException { |
304 StatusCommand st = new StatusCommand(settings); | |
305 st.setFromRevId(fromVer.getId()); | |
306 st.setToRevId(toVer.getId()); | |
307 List<ModifiedFile> modifiedFiles = st.execute(); | |
308 List<String> notDeletedFiles = new ArrayList<String>(); | |
309 for (ModifiedFile f: modifiedFiles) { | |
310 if (f.getStatus() != ModifiedFile.Status.REMOVED) { | |
311 notDeletedFiles.add(f.getPath()); | |
312 } | |
313 } | |
314 | |
47
c785bc4c5f39
minor fix: do not call cat command if there are no files to export
Pavel.Sher
parents:
46
diff
changeset
|
315 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
|
316 |
0 | 317 CatCommand cc = new CatCommand(settings); |
318 cc.setRevId(toVer.getId()); | |
319 File parentDir = cc.execute(notDeletedFiles); | |
320 | |
321 try { | |
322 for (ModifiedFile f: modifiedFiles) { | |
79 | 323 String mappedPath = checkoutRules.map(f.getPath()); |
324 if (mappedPath == null) continue; // skip | |
325 final File virtualFile = new File(mappedPath); | |
0 | 326 if (f.getStatus() == ModifiedFile.Status.REMOVED) { |
327 builder.deleteFile(virtualFile, true); | |
328 } else { | |
329 File realFile = new File(parentDir, f.getPath()); | |
330 FileInputStream is = new FileInputStream(realFile); | |
331 try { | |
11 | 332 builder.changeOrCreateBinaryFile(virtualFile, null, is, realFile.length()); |
0 | 333 } finally { |
334 is.close(); | |
335 } | |
336 } | |
337 } | |
338 } finally { | |
339 FileUtil.delete(parentDir); | |
340 } | |
341 } | |
342 | |
19 | 343 // builds patch by exporting files using specified version |
79 | 344 private void buildFullPatch(final Settings settings, @NotNull final ChangeSet toVer, final PatchBuilder builder, final CheckoutRules checkoutRules) |
0 | 345 throws IOException, VcsException { |
346 CloneCommand cl = new CloneCommand(settings); | |
57 | 347 // clone from the local repository |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
348 cl.setRepository(settings.getLocalRepositoryDir().getAbsolutePath()); |
0 | 349 cl.setToId(toVer.getId()); |
57 | 350 cl.setUpdateWorkingDir(false); |
0 | 351 File tempDir = FileUtil.createTempDirectory("mercurial", toVer.getId()); |
352 try { | |
353 final File repRoot = new File(tempDir, "rep"); | |
354 cl.setDestDir(repRoot.getAbsolutePath()); | |
355 cl.execute(); | |
57 | 356 |
357 UpdateCommand up = new UpdateCommand(settings); | |
358 up.setWorkDirectory(repRoot.getAbsolutePath()); | |
359 up.setToId(toVer.getId()); | |
360 up.execute(); | |
361 | |
0 | 362 buildPatchFromDirectory(builder, repRoot, new FileFilter() { |
363 public boolean accept(final File file) { | |
364 return !(file.isDirectory() && ".hg".equals(file.getName())); | |
365 } | |
79 | 366 }, checkoutRules); |
0 | 367 } finally { |
368 FileUtil.delete(tempDir); | |
369 } | |
370 } | |
371 | |
79 | 372 private void buildPatchFromDirectory(final PatchBuilder builder, final File repRoot, final FileFilter filter, final CheckoutRules checkoutRules) throws IOException { |
373 buildPatchFromDirectory(repRoot, builder, repRoot, filter, checkoutRules); | |
0 | 374 } |
375 | |
79 | 376 private void buildPatchFromDirectory(File curDir, final PatchBuilder builder, final File repRoot, final FileFilter filter, final CheckoutRules checkoutRules) throws IOException { |
0 | 377 File[] files = curDir.listFiles(filter); |
378 if (files != null) { | |
379 for (File realFile: files) { | |
380 String relPath = realFile.getAbsolutePath().substring(repRoot.getAbsolutePath().length()); | |
79 | 381 String mappedPath = checkoutRules.map(relPath); |
382 if (mappedPath != null) { | |
383 final File virtualFile = new File(mappedPath); | |
384 if (realFile.isDirectory()) { | |
385 builder.createDirectory(virtualFile); | |
386 buildPatchFromDirectory(realFile, builder, repRoot, filter, checkoutRules); | |
387 } else { | |
388 final FileInputStream is = new FileInputStream(realFile); | |
389 try { | |
390 builder.createBinaryFile(virtualFile, null, is, realFile.length()); | |
391 } finally { | |
392 is.close(); | |
393 } | |
0 | 394 } |
395 } | |
396 } | |
397 } | |
398 } | |
399 | |
19 | 400 // updates current working copy of repository by pulling changes from the repository specified in VCS root |
44 | 401 private void syncClonedRepository(final VcsRoot root) throws VcsException { |
402 Settings settings = createSettings(root); | |
57 | 403 File workDir = settings.getLocalRepositoryDir(); |
21 | 404 lockWorkDir(workDir); |
405 try { | |
29 | 406 if (settings.hasCopyOfRepository()) { |
0 | 407 // update |
8 | 408 PullCommand pull = new PullCommand(settings); |
409 pull.execute(); | |
0 | 410 } else { |
411 // clone | |
412 CloneCommand cl = new CloneCommand(settings); | |
22 | 413 cl.setDestDir(workDir.getAbsolutePath()); |
18 | 414 cl.setUpdateWorkingDir(false); |
0 | 415 cl.execute(); |
416 } | |
21 | 417 } finally { |
418 unlockWorkDir(workDir); | |
0 | 419 } |
420 } | |
421 | |
44 | 422 @Override |
423 public LabelingSupport getLabelingSupport() { | |
424 return this; | |
425 } | |
426 | |
22 | 427 private void lockWorkDir(@NotNull File workDir) { |
21 | 428 getWorkDirLock(workDir).lock(); |
429 } | |
430 | |
22 | 431 private void unlockWorkDir(@NotNull File workDir) { |
21 | 432 getWorkDirLock(workDir).unlock(); |
433 } | |
434 | |
31 | 435 @Override |
86 | 436 public boolean ignoreServerCachesFor(@NotNull final VcsRoot root) { |
31 | 437 // since a copy of repository for each VCS root is already stored on disk |
438 // we do not need separate cache for our patches | |
439 return true; | |
440 } | |
441 | |
22 | 442 private Lock getWorkDirLock(final File workDir) { |
443 String path = workDir.getAbsolutePath(); | |
444 Lock lock = myWorkDirLocks.get(path); | |
21 | 445 if (lock == null) { |
446 lock = new ReentrantLock(); | |
22 | 447 Lock curLock = myWorkDirLocks.putIfAbsent(path, lock); |
21 | 448 if (curLock != null) { |
449 lock = curLock; | |
450 } | |
451 } | |
452 return lock; | |
453 } | |
454 | |
23 | 455 private void removeOldWorkFolders() { |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
456 File workFoldersParent = myDefaultWorkFolderParent; |
23 | 457 if (!workFoldersParent.isDirectory()) return; |
458 | |
459 Set<File> workDirs = new HashSet<File>(); | |
460 File[] files = workFoldersParent.listFiles(new FileFilter() { | |
461 public boolean accept(final File file) { | |
462 return file.isDirectory() && file.getName().startsWith(Settings.DEFAULT_WORK_DIR_PREFIX); | |
463 } | |
464 }); | |
465 if (files != null) { | |
466 for (File f: files) { | |
46 | 467 workDirs.add(PathUtil.getCanonicalFile(f)); |
23 | 468 } |
469 } | |
470 | |
471 for (VcsRoot vcsRoot: myVcsManager.getAllRegisteredVcsRoots()) { | |
472 if (getName().equals(vcsRoot.getVcsName())) { | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
473 try { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
474 Settings s = createSettings(vcsRoot); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
475 workDirs.remove(PathUtil.getCanonicalFile(s.getLocalRepositoryDir())); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
476 } catch (VcsException e) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
477 Loggers.VCS.error(e); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
478 } |
23 | 479 } |
480 } | |
481 | |
482 for (File f: workDirs) { | |
483 lockWorkDir(f); | |
484 try { | |
485 FileUtil.delete(f); | |
486 } finally { | |
487 unlockWorkDir(f); | |
488 } | |
489 } | |
490 } | |
44 | 491 |
492 public String label(@NotNull String label, @NotNull String version, @NotNull VcsRoot root, @NotNull CheckoutRules checkoutRules) throws VcsException { | |
493 syncClonedRepository(root); | |
494 | |
495 Settings settings = createSettings(root); | |
496 | |
497 // I do not know why but hg tag does not work correctly if | |
498 // update command was not invoked for the current repo | |
499 // in such case if there were no tags before Mercurial attempts to | |
500 // create new head when tag is pushed to the parent repository | |
501 UpdateCommand uc = new UpdateCommand(settings); | |
502 uc.execute(); | |
503 | |
504 String fixedTagname = fixTagName(label); | |
505 TagCommand tc = new TagCommand(settings); | |
506 tc.setRevId(new ChangeSet(version).getId()); | |
507 tc.setTag(fixedTagname); | |
508 tc.execute(); | |
509 | |
510 PushCommand pc = new PushCommand(settings); | |
57 | 511 pc.setForce(true); |
44 | 512 pc.execute(); |
513 return fixedTagname; | |
514 } | |
515 | |
516 private String fixTagName(final String label) { | |
517 // according to Mercurial documentation http://hgbook.red-bean.com/hgbookch8.html#x12-1570008 | |
518 // tag name must not contain: | |
519 // Colon (ASCII 58, “:”) | |
520 // Carriage return (ASCII 13, “\r”) | |
521 // Newline (ASCII 10, “\n”) | |
522 // all these characters will be replaced with _ (underscore) | |
523 return label.replace(':', '_').replace('\r', '_').replace('\n', '_'); | |
524 } | |
525 | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
526 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
|
527 Settings settings = new Settings(myDefaultWorkFolderParent, root); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
528 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
|
529 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
|
530 File parentDir = new File(customClonePath); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
531 createClonedRepositoryParentDir(parentDir); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
532 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
533 // take last part of repository path |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
534 String repPath = settings.getRepositoryUrl(); |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
535 String[] splitted = repPath.split("[/\\\\]"); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
536 if (splitted.length > 0) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
537 repPath = splitted[splitted.length-1]; |
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 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
540 File customWorkingDir = new File(parentDir, repPath); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
541 settings.setWorkingDir(customWorkingDir); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
542 } else { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
543 createClonedRepositoryParentDir(myDefaultWorkFolderParent); |
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 return settings; |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
546 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
547 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
548 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
|
549 if (!parentDir.exists() && !parentDir.mkdirs()) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
550 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
|
551 } |
44 | 552 } |
48 | 553 |
554 public boolean isAgentSideCheckoutAvailable() { | |
555 return true; | |
556 } | |
0 | 557 } |