Mercurial > hg > mercurial
annotate mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 27:7944e8985ebd
prepare modules structure for agent side checkout
author | Pavel.Sher |
---|---|
date | Wed, 23 Jul 2008 09:18:03 +0400 |
parents | mercurial/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java@7047f643747f |
children | a7cab5083ada |
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 */ | |
0 | 56 public class MercurialVcsSupport extends VcsSupport implements CollectChangesByIncludeRule { |
57 private ServerPaths myServerPaths; | |
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); |
67 myServerPaths = paths; | |
21 | 68 myVcsManager = vcsManager; |
69 server.getExecutor().scheduleAtFixedRate(new Runnable() { | |
70 public void run() { | |
71 removeOldWorkFolders(); | |
72 } | |
73 }, 0, OLD_WORK_DIRS_CLEANUP_PERIOD, TimeUnit.SECONDS); | |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
74 myDefaultWorkFolderParent = new File(paths.getCachesDir()); |
0 | 75 } |
76 | |
77 public List<ModificationData> collectBuildChanges(final VcsRoot root, | |
78 @NotNull final String fromVersion, | |
79 @NotNull final String currentVersion, | |
80 final CheckoutRules checkoutRules) throws VcsException { | |
81 updateWorkingDirectory(root); | |
82 return VcsSupportUtil.collectBuildChanges(root, fromVersion, currentVersion, checkoutRules, this); | |
83 } | |
84 | |
85 public List<ModificationData> collectBuildChanges(final VcsRoot root, | |
86 final String fromVersion, | |
87 final String currentVersion, | |
88 final IncludeRule includeRule) throws VcsException { | |
19 | 89 // first obtain changes between specified versions |
0 | 90 List<ModificationData> result = new ArrayList<ModificationData>(); |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
91 Settings settings = new Settings(myDefaultWorkFolderParent, root); |
0 | 92 LogCommand lc = new LogCommand(settings); |
93 lc.setFromRevId(new ChangeSet(fromVersion).getId()); | |
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 Iterator<ChangeSet> it = changeSets.iterator(); |
102 ChangeSet prev = it.next(); // skip first changeset (cause it was already reported) | |
103 StatusCommand st = new StatusCommand(settings); | |
104 while (it.hasNext()) { | |
105 ChangeSet cur = it.next(); | |
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; | |
113 ModificationData md = new ModificationData(cur.getTimestamp(), files, cur.getSummary(), cur.getUser(), root, cur.getFullVersion(), cur.getFullVersion()); | |
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 | |
147 public byte[] getContent(final VcsModification vcsModification, | |
148 final VcsChangeInfo change, | |
149 final VcsChangeInfo.ContentType contentType, | |
150 final VcsRoot vcsRoot) throws VcsException { | |
12 | 151 updateWorkingDirectory(vcsRoot); |
152 String version = contentType == VcsChangeInfo.ContentType.AFTER_CHANGE ? change.getAfterChangeRevisionNumber() : change.getBeforeChangeRevisionNumber(); | |
153 return getContent(change.getRelativeFileName(), vcsRoot, version); | |
0 | 154 } |
155 | |
156 @NotNull | |
12 | 157 public byte[] getContent(final String filePath, final VcsRoot vcsRoot, final String version) throws VcsException { |
158 updateWorkingDirectory(vcsRoot); | |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
159 Settings settings = new Settings(myDefaultWorkFolderParent, vcsRoot); |
12 | 160 CatCommand cc = new CatCommand(settings); |
161 ChangeSet cs = new ChangeSet(version); | |
162 cc.setRevId(cs.getId()); | |
163 File parentDir = cc.execute(Collections.singletonList(filePath)); | |
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); | |
173 } | |
0 | 174 return new byte[0]; |
175 } | |
176 | |
177 public String getName() { | |
178 return "mercurial"; | |
179 } | |
180 | |
181 @Used("jsp") | |
182 public String getDisplayName() { | |
183 return "Mercurial"; | |
184 } | |
185 | |
186 @Nullable | |
187 public PropertiesProcessor getVcsPropertiesProcessor() { | |
188 return new AbstractVcsPropertiesProcessor() { | |
189 public Collection<InvalidProperty> process(final Map<String, String> properties) { | |
190 List<InvalidProperty> result = new ArrayList<InvalidProperty>(); | |
191 if (isEmpty(properties.get(Constants.HG_COMMAND_PATH_PROP))) { | |
192 result.add(new InvalidProperty(Constants.HG_COMMAND_PATH_PROP, "Path to 'hg' command must be specified")); | |
193 } | |
194 if (isEmpty(properties.get(Constants.REPOSITORY_PROP))) { | |
195 result.add(new InvalidProperty(Constants.REPOSITORY_PROP, "Repository must be specified")); | |
196 } | |
197 return result; | |
198 } | |
199 }; | |
200 } | |
201 | |
202 public String getVcsSettingsJspFilePath() { | |
203 return "mercurialSettings.jsp"; | |
204 } | |
205 | |
206 @NotNull | |
207 public String getCurrentVersion(final VcsRoot root) throws VcsException { | |
19 | 208 // we will return full version of the most recent change as current version |
0 | 209 updateWorkingDirectory(root); |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
210 Settings settings = new Settings(myDefaultWorkFolderParent, root); |
9
7dadebd03515
use tip command instead of log in getCurrentVersion method
Pavel.Sher
parents:
8
diff
changeset
|
211 TipCommand lc = new TipCommand(settings); |
7dadebd03515
use tip command instead of log in getCurrentVersion method
Pavel.Sher
parents:
8
diff
changeset
|
212 ChangeSet changeSet = lc.execute(); |
0 | 213 return changeSet.getFullVersion(); |
214 } | |
215 | |
216 public String describeVcsRoot(final VcsRoot vcsRoot) { | |
217 return "mercurial: " + vcsRoot.getProperty(Constants.REPOSITORY_PROP); | |
218 } | |
219 | |
220 public boolean isTestConnectionSupported() { | |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
221 return true; |
0 | 222 } |
223 | |
224 @Nullable | |
225 public String testConnection(final VcsRoot vcsRoot) throws VcsException { | |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
226 Settings settings = new Settings(myDefaultWorkFolderParent, vcsRoot); |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
227 IdentifyCommand id = new IdentifyCommand(settings); |
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
228 StringBuilder res = new StringBuilder(); |
17 | 229 res.append(quoteIfNeeded(settings.getHgCommandPath())); |
230 res.append(" identify "); | |
231 res.append(quoteIfNeeded(settings.getRepository())); | |
16
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
232 res.append('\n').append(id.execute()); |
7aa397165fa0
identify command added, test connection now uses identify command
Pavel.Sher
parents:
13
diff
changeset
|
233 return res.toString(); |
0 | 234 } |
235 | |
17 | 236 private String quoteIfNeeded(@NotNull String str) { |
237 if (str.indexOf(' ') != -1) { | |
238 return "\"" + str + "\""; | |
239 } | |
240 | |
241 return str; | |
242 } | |
243 | |
0 | 244 @Nullable |
245 public Map<String, String> getDefaultVcsProperties() { | |
19 | 246 return Collections.singletonMap(Constants.HG_COMMAND_PATH_PROP, "hg"); |
0 | 247 } |
248 | |
249 public String getVersionDisplayName(final String version, final VcsRoot root) throws VcsException { | |
250 return version; | |
251 } | |
252 | |
253 @NotNull | |
254 public Comparator<String> getVersionComparator() { | |
19 | 255 // comparator is called when TeamCity needs to sort modifications in the order of their appearance, |
256 // currently we sort changes by revision number, not sure however that this is a good idea, | |
257 // probably it would be better to sort them by timestamp (and to add timestamp into the version). | |
0 | 258 return new Comparator<String>() { |
259 public int compare(final String o1, final String o2) { | |
260 try { | |
261 return new ChangeSet(o1).getRevNumber() - new ChangeSet(o2).getRevNumber(); | |
262 } catch (Exception e) { | |
263 return 1; | |
264 } | |
265 } | |
266 }; | |
267 } | |
268 | |
269 public void buildPatch(final VcsRoot root, | |
270 @Nullable final String fromVersion, | |
271 @NotNull final String toVersion, | |
272 final PatchBuilder builder, | |
273 final CheckoutRules checkoutRules) throws IOException, VcsException { | |
274 updateWorkingDirectory(root); | |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
275 Settings settings = new Settings(myDefaultWorkFolderParent, root); |
0 | 276 if (fromVersion == null) { |
277 buildFullPatch(settings, new ChangeSet(toVersion), builder); | |
278 } else { | |
279 buildIncrementalPatch(settings, new ChangeSet(fromVersion), new ChangeSet(toVersion), builder); | |
280 } | |
281 } | |
282 | |
19 | 283 // builds patch from version to version |
0 | 284 private void buildIncrementalPatch(final Settings settings, @NotNull final ChangeSet fromVer, @NotNull final ChangeSet toVer, final PatchBuilder builder) |
285 throws VcsException, IOException { | |
286 StatusCommand st = new StatusCommand(settings); | |
287 st.setFromRevId(fromVer.getId()); | |
288 st.setToRevId(toVer.getId()); | |
289 List<ModifiedFile> modifiedFiles = st.execute(); | |
290 List<String> notDeletedFiles = new ArrayList<String>(); | |
291 for (ModifiedFile f: modifiedFiles) { | |
292 if (f.getStatus() != ModifiedFile.Status.REMOVED) { | |
293 notDeletedFiles.add(f.getPath()); | |
294 } | |
295 } | |
296 | |
297 CatCommand cc = new CatCommand(settings); | |
298 cc.setRevId(toVer.getId()); | |
299 File parentDir = cc.execute(notDeletedFiles); | |
300 | |
301 try { | |
302 for (ModifiedFile f: modifiedFiles) { | |
303 final File virtualFile = new File(f.getPath()); | |
304 if (f.getStatus() == ModifiedFile.Status.REMOVED) { | |
305 builder.deleteFile(virtualFile, true); | |
306 } else { | |
307 File realFile = new File(parentDir, f.getPath()); | |
308 FileInputStream is = new FileInputStream(realFile); | |
309 try { | |
11 | 310 builder.changeOrCreateBinaryFile(virtualFile, null, is, realFile.length()); |
0 | 311 } finally { |
312 is.close(); | |
313 } | |
314 } | |
315 } | |
316 } finally { | |
317 FileUtil.delete(parentDir); | |
318 } | |
319 } | |
320 | |
19 | 321 // builds patch by exporting files using specified version |
0 | 322 private void buildFullPatch(final Settings settings, @NotNull final ChangeSet toVer, final PatchBuilder builder) |
323 throws IOException, VcsException { | |
324 CloneCommand cl = new CloneCommand(settings); | |
325 cl.setToId(toVer.getId()); | |
326 File tempDir = FileUtil.createTempDirectory("mercurial", toVer.getId()); | |
327 try { | |
328 final File repRoot = new File(tempDir, "rep"); | |
329 cl.setDestDir(repRoot.getAbsolutePath()); | |
330 cl.execute(); | |
331 buildPatchFromDirectory(builder, repRoot, new FileFilter() { | |
332 public boolean accept(final File file) { | |
333 return !(file.isDirectory() && ".hg".equals(file.getName())); | |
334 } | |
335 }); | |
336 } finally { | |
337 FileUtil.delete(tempDir); | |
338 } | |
339 } | |
340 | |
341 private void buildPatchFromDirectory(final PatchBuilder builder, final File repRoot, final FileFilter filter) throws IOException { | |
342 buildPatchFromDirectory(repRoot, builder, repRoot, filter); | |
343 } | |
344 | |
345 private void buildPatchFromDirectory(File curDir, final PatchBuilder builder, final File repRoot, final FileFilter filter) throws IOException { | |
346 File[] files = curDir.listFiles(filter); | |
347 if (files != null) { | |
348 for (File realFile: files) { | |
349 String relPath = realFile.getAbsolutePath().substring(repRoot.getAbsolutePath().length()); | |
350 final File virtualFile = new File(relPath); | |
351 if (realFile.isDirectory()) { | |
352 builder.createDirectory(virtualFile); | |
353 buildPatchFromDirectory(realFile, builder, repRoot, filter); | |
354 } else { | |
355 final FileInputStream is = new FileInputStream(realFile); | |
356 try { | |
357 builder.createBinaryFile(virtualFile, null, is, realFile.length()); | |
358 } finally { | |
359 is.close(); | |
360 } | |
361 } | |
362 } | |
363 } | |
364 } | |
365 | |
19 | 366 // updates current working copy of repository by pulling changes from the repository specified in VCS root |
0 | 367 private void updateWorkingDirectory(final VcsRoot root) throws VcsException { |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
368 Settings settings = new Settings(myDefaultWorkFolderParent, root); |
22 | 369 File workDir = settings.getWorkingDir(); |
21 | 370 lockWorkDir(workDir); |
371 try { | |
22 | 372 if (hasRepositoryCopy(workDir)) { |
0 | 373 // update |
8 | 374 PullCommand pull = new PullCommand(settings); |
375 pull.execute(); | |
0 | 376 } else { |
377 // clone | |
378 CloneCommand cl = new CloneCommand(settings); | |
22 | 379 cl.setDestDir(workDir.getAbsolutePath()); |
18 | 380 cl.setUpdateWorkingDir(false); |
0 | 381 cl.execute(); |
382 } | |
21 | 383 } finally { |
384 unlockWorkDir(workDir); | |
0 | 385 } |
386 } | |
387 | |
22 | 388 private void lockWorkDir(@NotNull File workDir) { |
21 | 389 getWorkDirLock(workDir).lock(); |
390 } | |
391 | |
22 | 392 private void unlockWorkDir(@NotNull File workDir) { |
21 | 393 getWorkDirLock(workDir).unlock(); |
394 } | |
395 | |
22 | 396 private Lock getWorkDirLock(final File workDir) { |
397 String path = workDir.getAbsolutePath(); | |
398 Lock lock = myWorkDirLocks.get(path); | |
21 | 399 if (lock == null) { |
400 lock = new ReentrantLock(); | |
22 | 401 Lock curLock = myWorkDirLocks.putIfAbsent(path, lock); |
21 | 402 if (curLock != null) { |
403 lock = curLock; | |
404 } | |
405 } | |
406 return lock; | |
407 } | |
408 | |
0 | 409 private boolean hasRepositoryCopy(final File workDir) { |
19 | 410 // need better way to check that repository copy is ok |
0 | 411 return workDir.isDirectory() && new File(workDir, ".hg").isDirectory(); |
412 } | |
413 | |
23 | 414 private void removeOldWorkFolders() { |
415 File workFoldersParent = new File(myServerPaths.getCachesDir(), "mercurial"); | |
416 if (!workFoldersParent.isDirectory()) return; | |
417 | |
418 Set<File> workDirs = new HashSet<File>(); | |
419 File[] files = workFoldersParent.listFiles(new FileFilter() { | |
420 public boolean accept(final File file) { | |
421 return file.isDirectory() && file.getName().startsWith(Settings.DEFAULT_WORK_DIR_PREFIX); | |
422 } | |
423 }); | |
424 if (files != null) { | |
425 for (File f: files) { | |
426 workDirs.add(FileUtil.getCanonicalFile(f)); | |
427 } | |
428 } | |
429 | |
430 for (VcsRoot vcsRoot: myVcsManager.getAllRegisteredVcsRoots()) { | |
431 if (getName().equals(vcsRoot.getVcsName())) { | |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
432 Settings s = new Settings(myDefaultWorkFolderParent, vcsRoot); |
23 | 433 workDirs.remove(FileUtil.getCanonicalFile(s.getWorkingDir())); |
434 } | |
435 } | |
436 | |
437 for (File f: workDirs) { | |
438 lockWorkDir(f); | |
439 try { | |
440 FileUtil.delete(f); | |
441 } finally { | |
442 unlockWorkDir(f); | |
443 } | |
444 } | |
445 } | |
0 | 446 } |