annotate mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 59:30cc10fe9479

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