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