0
|
1 package jetbrains.buildServer.buildTriggers.vcs.mercurial;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.FileFilter;
|
|
5 import java.io.FileInputStream;
|
|
6 import java.io.IOException;
|
|
7 import java.util.*;
|
|
8 import jetbrains.buildServer.CollectChangesByIncludeRule;
|
|
9 import jetbrains.buildServer.Used;
|
|
10 import jetbrains.buildServer.buildTriggers.vcs.AbstractVcsPropertiesProcessor;
|
|
11 import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
|
|
12 import jetbrains.buildServer.log.Loggers;
|
|
13 import jetbrains.buildServer.serverSide.InvalidProperty;
|
|
14 import jetbrains.buildServer.serverSide.PropertiesProcessor;
|
|
15 import jetbrains.buildServer.serverSide.ServerPaths;
|
|
16 import jetbrains.buildServer.util.FileUtil;
|
|
17 import jetbrains.buildServer.vcs.*;
|
|
18 import jetbrains.buildServer.vcs.patches.PatchBuilder;
|
|
19 import org.jetbrains.annotations.NotNull;
|
|
20 import org.jetbrains.annotations.Nullable;
|
|
21
|
|
22 public class MercurialVcsSupport extends VcsSupport implements CollectChangesByIncludeRule {
|
|
23 private ServerPaths myServerPaths;
|
|
24
|
|
25 public MercurialVcsSupport(@NotNull VcsManager vcsManager, @NotNull ServerPaths paths) {
|
|
26 vcsManager.registerVcsSupport(this);
|
|
27 myServerPaths = paths;
|
|
28 }
|
|
29
|
|
30 public List<ModificationData> collectBuildChanges(final VcsRoot root,
|
|
31 @NotNull final String fromVersion,
|
|
32 @NotNull final String currentVersion,
|
|
33 final CheckoutRules checkoutRules) throws VcsException {
|
|
34 updateWorkingDirectory(root);
|
|
35 return VcsSupportUtil.collectBuildChanges(root, fromVersion, currentVersion, checkoutRules, this);
|
|
36 }
|
|
37
|
|
38 public List<ModificationData> collectBuildChanges(final VcsRoot root,
|
|
39 final String fromVersion,
|
|
40 final String currentVersion,
|
|
41 final IncludeRule includeRule) throws VcsException {
|
|
42 List<ModificationData> result = new ArrayList<ModificationData>();
|
|
43 Settings settings = new Settings(myServerPaths, root);
|
|
44 LogCommand lc = new LogCommand(settings);
|
|
45 lc.setFromRevId(new ChangeSet(fromVersion).getId());
|
|
46 lc.setToRevId(new ChangeSet(currentVersion).getId());
|
|
47 List<ChangeSet> changeSets = lc.execute();
|
|
48 if (changeSets.isEmpty()) {
|
|
49 return result;
|
|
50 }
|
|
51
|
|
52 Iterator<ChangeSet> it = changeSets.iterator();
|
|
53 ChangeSet prev = it.next(); // skip first changeset (cause it was already reported)
|
|
54 StatusCommand st = new StatusCommand(settings);
|
|
55 while (it.hasNext()) {
|
|
56 ChangeSet cur = it.next();
|
|
57 st.setFromRevId(prev.getId());
|
|
58 st.setToRevId(cur.getId());
|
|
59 List<ModifiedFile> modifiedFiles = st.execute();
|
|
60 List<VcsChange> files = toVcsChanges(modifiedFiles, prev.getFullVersion(), cur.getFullVersion(), includeRule);
|
|
61 if (files.isEmpty()) continue;
|
|
62 ModificationData md = new ModificationData(cur.getTimestamp(), files, cur.getSummary(), cur.getUser(), root, cur.getFullVersion(), cur.getFullVersion());
|
|
63 result.add(md);
|
|
64 prev = cur;
|
|
65 }
|
|
66
|
|
67 return result;
|
|
68 }
|
|
69
|
|
70 private List<VcsChange> toVcsChanges(final List<ModifiedFile> modifiedFiles, String prevVer, String curVer, final IncludeRule includeRule) {
|
|
71 List<VcsChange> files = new ArrayList<VcsChange>();
|
|
72 for (ModifiedFile mf: modifiedFiles) {
|
|
73 if (!normalizePath(mf.getPath()).startsWith(includeRule.getFrom())) continue; // skip files which do not match include rule
|
|
74
|
|
75 VcsChangeInfo.Type changeType = getChangeType(mf.getStatus());
|
|
76 if (changeType == null) {
|
|
77 Loggers.VCS.warn("Unable to convert status: " + mf.getStatus() + " to VCS change type");
|
|
78 changeType = VcsChangeInfo.Type.NOT_CHANGED;
|
|
79 }
|
|
80 files.add(new VcsChange(changeType, mf.getStatus().getName(), mf.getPath(), mf.getPath(), prevVer, curVer));
|
|
81 }
|
|
82 return files;
|
|
83 }
|
|
84
|
|
85 private @NotNull String normalizePath(@NotNull String repPath) {
|
|
86 return repPath.replace('\\', '/');
|
|
87 }
|
|
88
|
|
89 private VcsChangeInfo.Type getChangeType(final ModifiedFile.Status status) {
|
|
90 switch (status) {
|
|
91 case ADDED:return VcsChangeInfo.Type.ADDED;
|
|
92 case MODIFIED:return VcsChangeInfo.Type.CHANGED;
|
|
93 case REMOVED:return VcsChangeInfo.Type.REMOVED;
|
|
94 }
|
|
95 return null;
|
|
96 }
|
|
97
|
|
98 @NotNull
|
|
99 public byte[] getContent(final VcsModification vcsModification,
|
|
100 final VcsChangeInfo change,
|
|
101 final VcsChangeInfo.ContentType contentType,
|
|
102 final VcsRoot vcsRoot) throws VcsException {
|
|
103 return new byte[0];
|
|
104 }
|
|
105
|
|
106 @NotNull
|
|
107 public byte[] getContent(final String filePath, final VcsRoot versionedRoot, final String version) throws VcsException {
|
|
108 return new byte[0];
|
|
109 }
|
|
110
|
|
111 public String getName() {
|
|
112 return "mercurial";
|
|
113 }
|
|
114
|
|
115 @Used("jsp")
|
|
116 public String getDisplayName() {
|
|
117 return "Mercurial";
|
|
118 }
|
|
119
|
|
120 @Nullable
|
|
121 public PropertiesProcessor getVcsPropertiesProcessor() {
|
|
122 return new AbstractVcsPropertiesProcessor() {
|
|
123 public Collection<InvalidProperty> process(final Map<String, String> properties) {
|
|
124 List<InvalidProperty> result = new ArrayList<InvalidProperty>();
|
|
125 if (isEmpty(properties.get(Constants.HG_COMMAND_PATH_PROP))) {
|
|
126 result.add(new InvalidProperty(Constants.HG_COMMAND_PATH_PROP, "Path to 'hg' command must be specified"));
|
|
127 }
|
|
128 if (isEmpty(properties.get(Constants.REPOSITORY_PROP))) {
|
|
129 result.add(new InvalidProperty(Constants.REPOSITORY_PROP, "Repository must be specified"));
|
|
130 }
|
|
131 return result;
|
|
132 }
|
|
133 };
|
|
134 }
|
|
135
|
|
136 public String getVcsSettingsJspFilePath() {
|
|
137 return "mercurialSettings.jsp";
|
|
138 }
|
|
139
|
|
140 @NotNull
|
|
141 public String getCurrentVersion(final VcsRoot root) throws VcsException {
|
|
142 updateWorkingDirectory(root);
|
|
143 Settings settings = new Settings(myServerPaths, root);
|
|
144 LogCommand lc = new LogCommand(settings);
|
|
145 lc.setFromRevId("tip");
|
|
146 lc.setToRevId("tip");
|
|
147 List<ChangeSet> changes = lc.execute();
|
|
148 if (changes.isEmpty()) {
|
|
149 throw new VcsException("Unable to obtain current version of repository");
|
|
150 }
|
|
151 final ChangeSet changeSet = changes.get(0);
|
|
152 return changeSet.getFullVersion();
|
|
153 }
|
|
154
|
|
155 public String describeVcsRoot(final VcsRoot vcsRoot) {
|
|
156 return "mercurial: " + vcsRoot.getProperty(Constants.REPOSITORY_PROP);
|
|
157 }
|
|
158
|
|
159 public boolean isTestConnectionSupported() {
|
|
160 return true;
|
|
161 }
|
|
162
|
|
163 @Nullable
|
|
164 public String testConnection(final VcsRoot vcsRoot) throws VcsException {
|
|
165 getCurrentVersion(vcsRoot);
|
|
166 return null;
|
|
167 }
|
|
168
|
|
169 @Nullable
|
|
170 public Map<String, String> getDefaultVcsProperties() {
|
|
171 return null;
|
|
172 }
|
|
173
|
|
174 public String getVersionDisplayName(final String version, final VcsRoot root) throws VcsException {
|
|
175 return version;
|
|
176 }
|
|
177
|
|
178 @NotNull
|
|
179 public Comparator<String> getVersionComparator() {
|
|
180 return new Comparator<String>() {
|
|
181 public int compare(final String o1, final String o2) {
|
|
182 try {
|
|
183 return new ChangeSet(o1).getRevNumber() - new ChangeSet(o2).getRevNumber();
|
|
184 } catch (Exception e) {
|
|
185 return 1;
|
|
186 }
|
|
187 }
|
|
188 };
|
|
189 }
|
|
190
|
|
191 public void buildPatch(final VcsRoot root,
|
|
192 @Nullable final String fromVersion,
|
|
193 @NotNull final String toVersion,
|
|
194 final PatchBuilder builder,
|
|
195 final CheckoutRules checkoutRules) throws IOException, VcsException {
|
|
196 updateWorkingDirectory(root);
|
|
197 Settings settings = new Settings(myServerPaths, root);
|
|
198 if (fromVersion == null) {
|
|
199 buildFullPatch(settings, new ChangeSet(toVersion), builder);
|
|
200 } else {
|
|
201 buildIncrementalPatch(settings, new ChangeSet(fromVersion), new ChangeSet(toVersion), builder);
|
|
202 }
|
|
203 }
|
|
204
|
|
205 private void buildIncrementalPatch(final Settings settings, @NotNull final ChangeSet fromVer, @NotNull final ChangeSet toVer, final PatchBuilder builder)
|
|
206 throws VcsException, IOException {
|
|
207 StatusCommand st = new StatusCommand(settings);
|
|
208 st.setFromRevId(fromVer.getId());
|
|
209 st.setToRevId(toVer.getId());
|
|
210 List<ModifiedFile> modifiedFiles = st.execute();
|
|
211 List<String> notDeletedFiles = new ArrayList<String>();
|
|
212 for (ModifiedFile f: modifiedFiles) {
|
|
213 if (f.getStatus() != ModifiedFile.Status.REMOVED) {
|
|
214 notDeletedFiles.add(f.getPath());
|
|
215 }
|
|
216 }
|
|
217
|
|
218 CatCommand cc = new CatCommand(settings);
|
|
219 cc.setRevId(toVer.getId());
|
|
220 File parentDir = cc.execute(notDeletedFiles);
|
|
221
|
|
222 try {
|
|
223 for (ModifiedFile f: modifiedFiles) {
|
|
224 final File virtualFile = new File(f.getPath());
|
|
225 if (f.getStatus() == ModifiedFile.Status.REMOVED) {
|
|
226 builder.deleteFile(virtualFile, true);
|
|
227 } else {
|
|
228 File realFile = new File(parentDir, f.getPath());
|
|
229 FileInputStream is = new FileInputStream(realFile);
|
|
230 try {
|
|
231 builder.createBinaryFile(virtualFile, null, is, realFile.length());
|
|
232 } finally {
|
|
233 is.close();
|
|
234 }
|
|
235 }
|
|
236 }
|
|
237 } finally {
|
|
238 FileUtil.delete(parentDir);
|
|
239 }
|
|
240 }
|
|
241
|
|
242 private void buildFullPatch(final Settings settings, @NotNull final ChangeSet toVer, final PatchBuilder builder)
|
|
243 throws IOException, VcsException {
|
|
244 CloneCommand cl = new CloneCommand(settings);
|
|
245 cl.setToId(toVer.getId());
|
|
246 File tempDir = FileUtil.createTempDirectory("mercurial", toVer.getId());
|
|
247 try {
|
|
248 final File repRoot = new File(tempDir, "rep");
|
|
249 cl.setDestDir(repRoot.getAbsolutePath());
|
|
250 cl.execute();
|
|
251 buildPatchFromDirectory(builder, repRoot, new FileFilter() {
|
|
252 public boolean accept(final File file) {
|
|
253 return !(file.isDirectory() && ".hg".equals(file.getName()));
|
|
254 }
|
|
255 });
|
|
256 } finally {
|
|
257 FileUtil.delete(tempDir);
|
|
258 }
|
|
259 }
|
|
260
|
|
261 private void buildPatchFromDirectory(final PatchBuilder builder, final File repRoot, final FileFilter filter) throws IOException {
|
|
262 buildPatchFromDirectory(repRoot, builder, repRoot, filter);
|
|
263 }
|
|
264
|
|
265 private void buildPatchFromDirectory(File curDir, final PatchBuilder builder, final File repRoot, final FileFilter filter) throws IOException {
|
|
266 File[] files = curDir.listFiles(filter);
|
|
267 if (files != null) {
|
|
268 for (File realFile: files) {
|
|
269 String relPath = realFile.getAbsolutePath().substring(repRoot.getAbsolutePath().length());
|
|
270 final File virtualFile = new File(relPath);
|
|
271 if (realFile.isDirectory()) {
|
|
272 builder.createDirectory(virtualFile);
|
|
273 buildPatchFromDirectory(realFile, builder, repRoot, filter);
|
|
274 } else {
|
|
275 final FileInputStream is = new FileInputStream(realFile);
|
|
276 try {
|
|
277 builder.createBinaryFile(virtualFile, null, is, realFile.length());
|
|
278 } finally {
|
|
279 is.close();
|
|
280 }
|
|
281 }
|
|
282 }
|
|
283 }
|
|
284 }
|
|
285
|
|
286 private void updateWorkingDirectory(final VcsRoot root) throws VcsException {
|
|
287 Settings settings = new Settings(myServerPaths, root);
|
|
288 String workDir = settings.getWorkingDir();
|
|
289 synchronized (root) {
|
|
290 if (hasRepositoryCopy(new File(workDir))) {
|
|
291 // update
|
|
292 UpdateCommand up = new UpdateCommand(settings);
|
|
293 up.execute();
|
|
294 } else {
|
|
295 // clone
|
|
296 CloneCommand cl = new CloneCommand(settings);
|
|
297 cl.setDestDir(workDir);
|
|
298 cl.execute();
|
|
299 }
|
|
300 }
|
|
301 }
|
|
302
|
|
303 private boolean hasRepositoryCopy(final File workDir) {
|
|
304 return workDir.isDirectory() && new File(workDir, ".hg").isDirectory();
|
|
305 }
|
|
306
|
|
307 }
|