Mercurial > hg > mercurial
annotate mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 144:2e90ef872b68
Collect changes using CheckoutRules.
We built patches with CheckoutRules anyway.
Also BranchSupport interface currently allows collecting changes
between 2 roots only using CheckoutRules. And this will have better
performance.
author | Dmitry Neverov <dmitry.neverov@jetbrains.com> |
---|---|
date | Tue, 11 Jan 2011 13:05:31 +0300 |
parents | 2ab0bce4bf64 |
children | 3a8af53dea6b 403d16397f5c |
rev | line source |
---|---|
25 | 1 /* |
95 | 2 * Copyright 2000-2010 JetBrains s.r.o. |
25 | 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 | |
103 | 18 import jetbrains.buildServer.BuildAgent; |
0 | 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; | |
103 | 23 import jetbrains.buildServer.serverSide.*; |
24 import jetbrains.buildServer.util.EventDispatcher; | |
0 | 25 import jetbrains.buildServer.util.FileUtil; |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
26 import jetbrains.buildServer.util.StringUtil; |
103 | 27 import jetbrains.buildServer.util.filters.Filter; |
28 import jetbrains.buildServer.util.filters.FilterUtil; | |
0 | 29 import jetbrains.buildServer.vcs.*; |
30 import jetbrains.buildServer.vcs.patches.PatchBuilder; | |
31 import org.jetbrains.annotations.NotNull; | |
32 import org.jetbrains.annotations.Nullable; | |
33 | |
1 | 34 import java.io.File; |
35 import java.io.FileFilter; | |
36 import java.io.FileInputStream; | |
37 import java.io.IOException; | |
38 import java.util.*; | |
21 | 39 import java.util.concurrent.ConcurrentHashMap; |
40 import java.util.concurrent.ConcurrentMap; | |
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 * | |
57 | 52 * <p>Working copy of repository is created in the $TEAMCITY_DATA_PATH/system/caches/hg_<hash code> folder. |
19 | 53 * <p>Personal builds (remote runs) are not yet supported, they require corresponding functionality from the IDE. |
54 */ | |
106 | 55 public class MercurialVcsSupport extends ServerVcsSupport implements LabelingSupport, VcsFileContentProvider { |
21 | 56 private ConcurrentMap<String, Lock> myWorkDirLocks= new ConcurrentHashMap<String, Lock>(); |
57 private VcsManager myVcsManager; | |
27
7944e8985ebd
prepare modules structure for agent side checkout
Pavel.Sher
parents:
25
diff
changeset
|
58 private File myDefaultWorkFolderParent; |
0 | 59 |
21 | 60 public MercurialVcsSupport(@NotNull final VcsManager vcsManager, |
61 @NotNull ServerPaths paths, | |
103 | 62 @NotNull final SBuildServer server, |
63 @NotNull EventDispatcher<BuildServerListener> dispatcher) { | |
21 | 64 myVcsManager = vcsManager; |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
65 myDefaultWorkFolderParent = new File(paths.getCachesDir(), "mercurial"); |
103 | 66 dispatcher.addListener(new BuildServerAdapter() { |
67 @Override | |
121 | 68 public void cleanupFinished() { |
69 super.cleanupFinished(); | |
70 server.getExecutor().submit(new Runnable() { | |
71 public void run() { | |
72 removeOldWorkFolders(); | |
73 } | |
74 }); | |
75 } | |
76 | |
77 @Override | |
103 | 78 public void sourcesVersionReleased(@NotNull final BuildAgent agent) { |
79 super.sourcesVersionReleased(agent); | |
80 server.getExecutor().submit(new Runnable() { | |
81 public void run() { | |
82 Set<File> clonedRepos = getAllClonedRepos(); | |
83 if (clonedRepos == null) return; | |
84 for (File f: clonedRepos) { | |
85 lockWorkDir(f); | |
86 try { | |
87 FileUtil.delete(f); | |
88 } finally { | |
89 unlockWorkDir(f); | |
90 } | |
91 } | |
92 } | |
93 }); | |
94 } | |
95 }); | |
0 | 96 } |
97 | |
90 | 98 private Collection<ModifiedFile> computeModifiedFilesForMergeCommit(final Settings settings, final ChangeSet cur) throws VcsException { |
99 ChangedFilesCommand cfc = new ChangedFilesCommand(settings); | |
100 cfc.setRevId(cur.getId()); | |
101 return cfc.execute(); | |
102 } | |
103 | |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
104 private List<VcsChange> toVcsChanges(final List<ModifiedFile> modifiedFiles, String prevVer, String curVer) { |
0 | 105 List<VcsChange> files = new ArrayList<VcsChange>(); |
106 for (ModifiedFile mf: modifiedFiles) { | |
107 VcsChangeInfo.Type changeType = getChangeType(mf.getStatus()); | |
108 if (changeType == null) { | |
109 Loggers.VCS.warn("Unable to convert status: " + mf.getStatus() + " to VCS change type"); | |
110 changeType = VcsChangeInfo.Type.NOT_CHANGED; | |
111 } | |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
112 files.add(new VcsChange(changeType, mf.getStatus().getName(), mf.getPath(), mf.getPath(), prevVer, curVer)); |
0 | 113 } |
114 return files; | |
115 } | |
116 | |
117 private VcsChangeInfo.Type getChangeType(final ModifiedFile.Status status) { | |
118 switch (status) { | |
119 case ADDED:return VcsChangeInfo.Type.ADDED; | |
120 case MODIFIED:return VcsChangeInfo.Type.CHANGED; | |
121 case REMOVED:return VcsChangeInfo.Type.REMOVED; | |
122 } | |
123 return null; | |
124 } | |
125 | |
126 @NotNull | |
86 | 127 public byte[] getContent(@NotNull final VcsModification vcsModification, |
128 @NotNull final VcsChangeInfo change, | |
129 @NotNull final VcsChangeInfo.ContentType contentType, | |
130 @NotNull final VcsRoot vcsRoot) throws VcsException { | |
44 | 131 syncClonedRepository(vcsRoot); |
12 | 132 String version = contentType == VcsChangeInfo.ContentType.AFTER_CHANGE ? change.getAfterChangeRevisionNumber() : change.getBeforeChangeRevisionNumber(); |
133 return getContent(change.getRelativeFileName(), vcsRoot, version); | |
0 | 134 } |
135 | |
136 @NotNull | |
86 | 137 public byte[] getContent(@NotNull final String filePath, @NotNull final VcsRoot vcsRoot, @NotNull final String version) throws VcsException { |
44 | 138 syncClonedRepository(vcsRoot); |
139 Settings settings = createSettings(vcsRoot); | |
12 | 140 CatCommand cc = new CatCommand(settings); |
59
30cc10fe9479
accept version in two formats: with rev number and without
Pavel.Sher
parents:
58
diff
changeset
|
141 cc.setRevId(new ChangeSet(version).getId()); |
12 | 142 File parentDir = cc.execute(Collections.singletonList(filePath)); |
30 | 143 try { |
144 File file = new File(parentDir, filePath); | |
145 if (file.isFile()) { | |
146 try { | |
147 return FileUtil.loadFileBytes(file); | |
148 } catch (IOException e) { | |
149 throw new VcsException("Failed to load content of file: " + file.getAbsolutePath(), e); | |
150 } | |
151 } else { | |
152 Loggers.VCS.warn("Unable to obtain content of the file: " + filePath); | |
12 | 153 } |
30 | 154 } finally { |
118
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
155 deleteTmpDir(parentDir); |
12 | 156 } |
0 | 157 return new byte[0]; |
158 } | |
159 | |
86 | 160 @NotNull |
0 | 161 public String getName() { |
28
a7cab5083ada
libraries moved on top level, dummy implementation of agent side checkout interface
Pavel.Sher
parents:
27
diff
changeset
|
162 return Constants.VCS_NAME; |
0 | 163 } |
164 | |
86 | 165 @NotNull |
0 | 166 @Used("jsp") |
167 public String getDisplayName() { | |
168 return "Mercurial"; | |
169 } | |
170 | |
171 @Nullable | |
172 public PropertiesProcessor getVcsPropertiesProcessor() { | |
173 return new AbstractVcsPropertiesProcessor() { | |
174 public Collection<InvalidProperty> process(final Map<String, String> properties) { | |
175 List<InvalidProperty> result = new ArrayList<InvalidProperty>(); | |
176 if (isEmpty(properties.get(Constants.HG_COMMAND_PATH_PROP))) { | |
177 result.add(new InvalidProperty(Constants.HG_COMMAND_PATH_PROP, "Path to 'hg' command must be specified")); | |
178 } | |
179 if (isEmpty(properties.get(Constants.REPOSITORY_PROP))) { | |
180 result.add(new InvalidProperty(Constants.REPOSITORY_PROP, "Repository must be specified")); | |
181 } | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
182 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
|
183 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
|
184 } |
0 | 185 return result; |
186 } | |
187 }; | |
188 } | |
189 | |
86 | 190 @NotNull |
0 | 191 public String getVcsSettingsJspFilePath() { |
192 return "mercurialSettings.jsp"; | |
193 } | |
194 | |
195 @NotNull | |
86 | 196 public String getCurrentVersion(@NotNull final VcsRoot root) throws VcsException { |
19 | 197 // we will return full version of the most recent change as current version |
44 | 198 syncClonedRepository(root); |
199 Settings settings = createSettings(root); | |
57 | 200 BranchesCommand branches = new BranchesCommand(settings); |
201 Map<String, ChangeSet> result = branches.execute(); | |
202 if (!result.containsKey(settings.getBranchName())) { | |
203 throw new VcsException("Unable to find current version for the branch: " + settings.getBranchName()); | |
204 } | |
205 | |
206 return result.get(settings.getBranchName()).getFullVersion(); | |
0 | 207 } |
208 | |
106 | 209 public boolean sourcesUpdatePossibleIfChangesNotFound(@NotNull final VcsRoot root) { |
210 return false; | |
211 } | |
212 | |
86 | 213 @NotNull |
0 | 214 public String describeVcsRoot(final VcsRoot vcsRoot) { |
215 return "mercurial: " + vcsRoot.getProperty(Constants.REPOSITORY_PROP); | |
216 } | |
217 | |
106 | 218 @Override |
219 public TestConnectionSupport getTestConnectionSupport() { | |
220 return new TestConnectionSupport() { | |
221 public String testConnection(@NotNull final VcsRoot vcsRoot) throws VcsException { | |
222 Settings settings = createSettings(vcsRoot); | |
223 IdentifyCommand id = new IdentifyCommand(settings); | |
224 StringBuilder res = new StringBuilder(); | |
225 res.append(quoteIfNeeded(settings.getHgCommandPath())); | |
226 res.append(" identify "); | |
227 final String obfuscatedUrl = CommandUtil.removePrivateData(settings.getRepositoryUrl(), Collections.singleton(settings.getPassword())); | |
228 res.append(quoteIfNeeded(obfuscatedUrl)); | |
229 res.append('\n').append(id.execute()); | |
230 return res.toString(); | |
231 } | |
232 }; | |
0 | 233 } |
234 | |
17 | 235 private String quoteIfNeeded(@NotNull String str) { |
236 if (str.indexOf(' ') != -1) { | |
237 return "\"" + str + "\""; | |
238 } | |
239 | |
240 return str; | |
241 } | |
242 | |
0 | 243 @Nullable |
244 public Map<String, String> getDefaultVcsProperties() { | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
245 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
|
246 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
|
247 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
|
248 return defaults; |
0 | 249 } |
250 | |
86 | 251 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
|
252 return new ChangeSet(version).getId(); |
0 | 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 | |
19 | 271 // builds patch from version to version |
79 | 272 private void buildIncrementalPatch(final Settings settings, @NotNull final ChangeSet fromVer, @NotNull final ChangeSet toVer, final PatchBuilder builder, final CheckoutRules checkoutRules) |
0 | 273 throws VcsException, IOException { |
274 StatusCommand st = new StatusCommand(settings); | |
275 st.setFromRevId(fromVer.getId()); | |
276 st.setToRevId(toVer.getId()); | |
277 List<ModifiedFile> modifiedFiles = st.execute(); | |
278 List<String> notDeletedFiles = new ArrayList<String>(); | |
279 for (ModifiedFile f: modifiedFiles) { | |
280 if (f.getStatus() != ModifiedFile.Status.REMOVED) { | |
281 notDeletedFiles.add(f.getPath()); | |
282 } | |
283 } | |
284 | |
47
c785bc4c5f39
minor fix: do not call cat command if there are no files to export
Pavel.Sher
parents:
46
diff
changeset
|
285 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
|
286 |
0 | 287 CatCommand cc = new CatCommand(settings); |
288 cc.setRevId(toVer.getId()); | |
289 File parentDir = cc.execute(notDeletedFiles); | |
290 | |
291 try { | |
292 for (ModifiedFile f: modifiedFiles) { | |
79 | 293 String mappedPath = checkoutRules.map(f.getPath()); |
294 if (mappedPath == null) continue; // skip | |
295 final File virtualFile = new File(mappedPath); | |
0 | 296 if (f.getStatus() == ModifiedFile.Status.REMOVED) { |
297 builder.deleteFile(virtualFile, true); | |
298 } else { | |
299 File realFile = new File(parentDir, f.getPath()); | |
300 FileInputStream is = new FileInputStream(realFile); | |
301 try { | |
11 | 302 builder.changeOrCreateBinaryFile(virtualFile, null, is, realFile.length()); |
0 | 303 } finally { |
304 is.close(); | |
305 } | |
306 } | |
307 } | |
308 } finally { | |
118
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
309 deleteTmpDir(parentDir); |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
310 } |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
311 } |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
312 |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
313 private void deleteTmpDir(File parentDir) { |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
314 boolean dirDeleted = FileUtil.delete(parentDir); |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
315 if (!dirDeleted) { |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
316 Loggers.VCS.warn("Can not delete directory \"" + parentDir.getAbsolutePath() + "\""); |
0 | 317 } |
318 } | |
319 | |
19 | 320 // builds patch by exporting files using specified version |
79 | 321 private void buildFullPatch(final Settings settings, @NotNull final ChangeSet toVer, final PatchBuilder builder, final CheckoutRules checkoutRules) |
0 | 322 throws IOException, VcsException { |
323 CloneCommand cl = new CloneCommand(settings); | |
57 | 324 // clone from the local repository |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
325 cl.setRepository(settings.getLocalRepositoryDir().getAbsolutePath()); |
0 | 326 cl.setToId(toVer.getId()); |
57 | 327 cl.setUpdateWorkingDir(false); |
0 | 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(); | |
57 | 333 |
334 UpdateCommand up = new UpdateCommand(settings); | |
335 up.setWorkDirectory(repRoot.getAbsolutePath()); | |
336 up.setToId(toVer.getId()); | |
337 up.execute(); | |
338 | |
0 | 339 buildPatchFromDirectory(builder, repRoot, new FileFilter() { |
340 public boolean accept(final File file) { | |
341 return !(file.isDirectory() && ".hg".equals(file.getName())); | |
342 } | |
79 | 343 }, checkoutRules); |
0 | 344 } finally { |
345 FileUtil.delete(tempDir); | |
346 } | |
347 } | |
348 | |
79 | 349 private void buildPatchFromDirectory(final PatchBuilder builder, final File repRoot, final FileFilter filter, final CheckoutRules checkoutRules) throws IOException { |
350 buildPatchFromDirectory(repRoot, builder, repRoot, filter, checkoutRules); | |
0 | 351 } |
352 | |
79 | 353 private void buildPatchFromDirectory(File curDir, final PatchBuilder builder, final File repRoot, final FileFilter filter, final CheckoutRules checkoutRules) throws IOException { |
0 | 354 File[] files = curDir.listFiles(filter); |
355 if (files != null) { | |
356 for (File realFile: files) { | |
357 String relPath = realFile.getAbsolutePath().substring(repRoot.getAbsolutePath().length()); | |
79 | 358 String mappedPath = checkoutRules.map(relPath); |
97 | 359 if (mappedPath != null && mappedPath.length() > 0) { |
79 | 360 final File virtualFile = new File(mappedPath); |
361 if (realFile.isDirectory()) { | |
362 builder.createDirectory(virtualFile); | |
363 buildPatchFromDirectory(realFile, builder, repRoot, filter, checkoutRules); | |
364 } else { | |
365 final FileInputStream is = new FileInputStream(realFile); | |
366 try { | |
367 builder.createBinaryFile(virtualFile, null, is, realFile.length()); | |
368 } finally { | |
369 is.close(); | |
370 } | |
0 | 371 } |
97 | 372 } else { |
373 if (realFile.isDirectory()) { | |
374 buildPatchFromDirectory(realFile, builder, repRoot, filter, checkoutRules); | |
375 } | |
0 | 376 } |
377 } | |
378 } | |
379 } | |
380 | |
19 | 381 // updates current working copy of repository by pulling changes from the repository specified in VCS root |
44 | 382 private void syncClonedRepository(final VcsRoot root) throws VcsException { |
383 Settings settings = createSettings(root); | |
57 | 384 File workDir = settings.getLocalRepositoryDir(); |
21 | 385 lockWorkDir(workDir); |
386 try { | |
29 | 387 if (settings.hasCopyOfRepository()) { |
0 | 388 // update |
8 | 389 PullCommand pull = new PullCommand(settings); |
390 pull.execute(); | |
0 | 391 } else { |
392 // clone | |
393 CloneCommand cl = new CloneCommand(settings); | |
22 | 394 cl.setDestDir(workDir.getAbsolutePath()); |
18 | 395 cl.setUpdateWorkingDir(false); |
0 | 396 cl.execute(); |
397 } | |
21 | 398 } finally { |
399 unlockWorkDir(workDir); | |
0 | 400 } |
401 } | |
402 | |
44 | 403 @Override |
404 public LabelingSupport getLabelingSupport() { | |
405 return this; | |
406 } | |
407 | |
106 | 408 @NotNull |
409 public VcsFileContentProvider getContentProvider() { | |
410 return this; | |
411 } | |
412 | |
413 @NotNull | |
414 public CollectChangesPolicy getCollectChangesPolicy() { | |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
415 return new CollectChangesByCheckoutRules() { |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
416 public List<ModificationData> collectChanges(@NotNull VcsRoot root, @NotNull String fromVersion, @Nullable String currentVersion, @NotNull CheckoutRules checkoutRules) throws VcsException { |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
417 syncClonedRepository(root); |
106 | 418 |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
419 // first obtain changes between specified versions |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
420 List<ModificationData> result = new ArrayList<ModificationData>(); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
421 if (currentVersion == null) return result; |
106 | 422 |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
423 Settings settings = createSettings(root); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
424 LogCommand lc = new LogCommand(settings); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
425 String fromId = new ChangeSetRevision(fromVersion).getId(); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
426 lc.setFromRevId(fromId); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
427 lc.setToRevId(new ChangeSetRevision(currentVersion).getId()); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
428 List<ChangeSet> changeSets = lc.execute(); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
429 if (changeSets.isEmpty()) { |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
430 return result; |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
431 } |
106 | 432 |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
433 // invoke status command for each changeset and determine what files were modified in these changesets |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
434 StatusCommand st = new StatusCommand(settings); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
435 ChangeSet prev = new ChangeSet(fromVersion); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
436 for (ChangeSet cur : changeSets) { |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
437 if (cur.getId().equals(fromId)) continue; // skip already reported changeset |
106 | 438 |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
439 String prevId = prev.getId(); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
440 List<ChangeSetRevision> curParents = cur.getParents(); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
441 boolean merge = curParents != null && curParents.size() > 1; |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
442 if (curParents != null && !merge) { |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
443 prevId = curParents.get(0).getId(); |
106 | 444 } |
445 | |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
446 List<ModifiedFile> modifiedFiles = new ArrayList<ModifiedFile>(); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
447 if (merge) { |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
448 modifiedFiles.addAll(computeModifiedFilesForMergeCommit(settings, cur)); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
449 } else { |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
450 st.setFromRevId(prevId); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
451 st.setToRevId(cur.getId()); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
452 modifiedFiles = st.execute(); |
106 | 453 } |
144
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
454 |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
455 // changeset full version will be set into VcsChange structure and |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
456 // stored in database (note that getContent method will be invoked with this version) |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
457 List<VcsChange> files = toVcsChanges(modifiedFiles, prev.getFullVersion(), cur.getFullVersion()); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
458 if (files.isEmpty() && !merge) continue; |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
459 ModificationData md = new ModificationData(cur.getTimestamp(), files, cur.getDescription(), cur.getUser(), root, cur.getFullVersion(), cur.getId()); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
460 if (merge) { |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
461 md.setCanBeIgnored(false); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
462 } |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
463 result.add(md); |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
464 prev = cur; |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
465 } |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
466 |
2e90ef872b68
Collect changes using CheckoutRules.
Dmitry Neverov <dmitry.neverov@jetbrains.com>
parents:
142
diff
changeset
|
467 return result; |
106 | 468 } |
469 }; | |
470 } | |
471 | |
472 @NotNull | |
473 public BuildPatchPolicy getBuildPatchPolicy() { | |
474 return new BuildPatchByCheckoutRules() { | |
475 public void buildPatch(@NotNull final VcsRoot root, | |
476 @Nullable final String fromVersion, | |
477 @NotNull final String toVersion, | |
478 @NotNull final PatchBuilder builder, | |
479 @NotNull final CheckoutRules checkoutRules) throws IOException, VcsException { | |
480 syncClonedRepository(root); | |
481 Settings settings = createSettings(root); | |
482 if (fromVersion == null) { | |
483 buildFullPatch(settings, new ChangeSet(toVersion), builder, checkoutRules); | |
484 } else { | |
485 buildIncrementalPatch(settings, new ChangeSet(fromVersion), new ChangeSet(toVersion), builder, checkoutRules); | |
486 } | |
487 } | |
488 }; | |
489 } | |
490 | |
22 | 491 private void lockWorkDir(@NotNull File workDir) { |
21 | 492 getWorkDirLock(workDir).lock(); |
493 } | |
494 | |
22 | 495 private void unlockWorkDir(@NotNull File workDir) { |
21 | 496 getWorkDirLock(workDir).unlock(); |
497 } | |
498 | |
31 | 499 @Override |
106 | 500 public boolean allowSourceCaching() { |
31 | 501 // since a copy of repository for each VCS root is already stored on disk |
106 | 502 // we do not need separate cache for our patches |
503 return false; | |
31 | 504 } |
505 | |
22 | 506 private Lock getWorkDirLock(final File workDir) { |
507 String path = workDir.getAbsolutePath(); | |
508 Lock lock = myWorkDirLocks.get(path); | |
21 | 509 if (lock == null) { |
510 lock = new ReentrantLock(); | |
22 | 511 Lock curLock = myWorkDirLocks.putIfAbsent(path, lock); |
21 | 512 if (curLock != null) { |
513 lock = curLock; | |
514 } | |
515 } | |
516 return lock; | |
517 } | |
518 | |
23 | 519 private void removeOldWorkFolders() { |
103 | 520 Set<File> workDirs = getAllClonedRepos(); |
521 if (workDirs == null) return; | |
522 | |
523 for (VcsRoot vcsRoot: getMercurialVcsRoots()) { | |
524 try { | |
525 Settings s = createSettings(vcsRoot); | |
526 workDirs.remove(PathUtil.getCanonicalFile(s.getLocalRepositoryDir())); | |
527 } catch (VcsException e) { | |
528 Loggers.VCS.error(e); | |
529 } | |
530 } | |
531 | |
532 for (File f: workDirs) { | |
533 lockWorkDir(f); | |
534 try { | |
535 FileUtil.delete(f); | |
536 } finally { | |
537 unlockWorkDir(f); | |
538 } | |
539 } | |
540 } | |
541 | |
542 private Collection<VcsRoot> getMercurialVcsRoots() { | |
543 List<VcsRoot> res = new ArrayList<VcsRoot>(myVcsManager.getAllRegisteredVcsRoots()); | |
544 FilterUtil.filterCollection(res, new Filter<VcsRoot>() { | |
545 public boolean accept(@NotNull final VcsRoot data) { | |
546 return getName().equals(data.getVcsName()); | |
547 } | |
548 }); | |
549 return res; | |
550 } | |
551 | |
552 @Nullable | |
553 private Set<File> getAllClonedRepos() { | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
554 File workFoldersParent = myDefaultWorkFolderParent; |
103 | 555 if (!workFoldersParent.isDirectory()) return null; |
23 | 556 |
557 Set<File> workDirs = new HashSet<File>(); | |
558 File[] files = workFoldersParent.listFiles(new FileFilter() { | |
559 public boolean accept(final File file) { | |
560 return file.isDirectory() && file.getName().startsWith(Settings.DEFAULT_WORK_DIR_PREFIX); | |
561 } | |
562 }); | |
563 if (files != null) { | |
564 for (File f: files) { | |
46 | 565 workDirs.add(PathUtil.getCanonicalFile(f)); |
23 | 566 } |
567 } | |
103 | 568 return workDirs; |
23 | 569 } |
44 | 570 |
571 public String label(@NotNull String label, @NotNull String version, @NotNull VcsRoot root, @NotNull CheckoutRules checkoutRules) throws VcsException { | |
572 syncClonedRepository(root); | |
573 | |
574 Settings settings = createSettings(root); | |
575 | |
576 // I do not know why but hg tag does not work correctly if | |
577 // update command was not invoked for the current repo | |
578 // in such case if there were no tags before Mercurial attempts to | |
579 // create new head when tag is pushed to the parent repository | |
580 UpdateCommand uc = new UpdateCommand(settings); | |
581 uc.execute(); | |
582 | |
583 String fixedTagname = fixTagName(label); | |
584 TagCommand tc = new TagCommand(settings); | |
585 tc.setRevId(new ChangeSet(version).getId()); | |
586 tc.setTag(fixedTagname); | |
587 tc.execute(); | |
588 | |
589 PushCommand pc = new PushCommand(settings); | |
101 | 590 // pc.setForce(true); |
44 | 591 pc.execute(); |
592 return fixedTagname; | |
593 } | |
594 | |
595 private String fixTagName(final String label) { | |
596 // according to Mercurial documentation http://hgbook.red-bean.com/hgbookch8.html#x12-1570008 | |
597 // tag name must not contain: | |
118
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
598 // Colon (ASCII 58, �:�) |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
599 // Carriage return (ASCII 13, �\r�) |
80eb7fdc4de0
Add logging for deleting tmp directories
nd@localhost.localdomain
parents:
106
diff
changeset
|
600 // Newline (ASCII 10, �\n�) |
44 | 601 // all these characters will be replaced with _ (underscore) |
602 return label.replace(':', '_').replace('\r', '_').replace('\n', '_'); | |
603 } | |
604 | |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
605 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
|
606 Settings settings = new Settings(myDefaultWorkFolderParent, root); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
607 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
|
608 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
|
609 File parentDir = new File(customClonePath); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
610 createClonedRepositoryParentDir(parentDir); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
611 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
612 // take last part of repository path |
67
e6971dc6b17c
always use url with credentials if username/password are specified
Pavel.Sher
parents:
62
diff
changeset
|
613 String repPath = settings.getRepositoryUrl(); |
62
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
614 String[] splitted = repPath.split("[/\\\\]"); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
615 if (splitted.length > 0) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
616 repPath = splitted[splitted.length-1]; |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
617 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
618 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
619 File customWorkingDir = new File(parentDir, repPath); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
620 settings.setWorkingDir(customWorkingDir); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
621 } else { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
622 createClonedRepositoryParentDir(myDefaultWorkFolderParent); |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
623 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
624 return settings; |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
625 } |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
626 |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
627 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
|
628 if (!parentDir.exists() && !parentDir.mkdirs()) { |
b328c6b6526d
TW-5636: Mercurial plugin can easilly hit Windows MAX_PATH limitations
Pavel.Sher
parents:
59
diff
changeset
|
629 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
|
630 } |
44 | 631 } |
48 | 632 |
633 public boolean isAgentSideCheckoutAvailable() { | |
634 return true; | |
635 } | |
0 | 636 } |