annotate mercurial/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 17:21b5b1c5dd74

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