Mercurial > jhg
comparison src/org/tmatesoft/hg/repo/ext/MqManager.java @ 471:7bcfbc255f48
Merge changes from smartgit3 branch into 1.1 stream
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 11 Jul 2012 20:40:47 +0200 |
parents | 2078692eeb58 |
children | 0e34b8f3946a |
comparison
equal
deleted
inserted
replaced
470:31bd09da0dcf | 471:7bcfbc255f48 |
---|---|
1 /* | |
2 * Copyright (c) 2012 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.repo.ext; | |
18 | |
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; | |
20 | |
21 import java.io.BufferedReader; | |
22 import java.io.File; | |
23 import java.io.FileReader; | |
24 import java.io.IOException; | |
25 import java.util.ArrayList; | |
26 import java.util.Collection; | |
27 import java.util.Collections; | |
28 import java.util.HashMap; | |
29 import java.util.LinkedList; | |
30 import java.util.List; | |
31 import java.util.Map; | |
32 | |
33 import org.tmatesoft.hg.core.Nodeid; | |
34 import org.tmatesoft.hg.repo.HgInternals; | |
35 import org.tmatesoft.hg.repo.HgInvalidControlFileException; | |
36 import org.tmatesoft.hg.repo.HgInvalidFileException; | |
37 import org.tmatesoft.hg.repo.HgRepository; | |
38 import org.tmatesoft.hg.util.LogFacility; | |
39 import org.tmatesoft.hg.util.Path; | |
40 | |
41 /** | |
42 * Mercurial Queues Support. | |
43 * Access to MqExtension functionality. | |
44 * | |
45 * @author Artem Tikhomirov | |
46 * @author TMate Software Ltd. | |
47 */ | |
48 public class MqManager { | |
49 | |
50 private static final String PATCHES_DIR = "patches"; | |
51 | |
52 private final HgRepository repo; | |
53 private List<PatchRecord> applied = Collections.emptyList(); | |
54 private List<PatchRecord> allKnown = Collections.emptyList(); | |
55 private List<String> queueNames = Collections.emptyList(); | |
56 private String activeQueue = PATCHES_DIR; | |
57 | |
58 public MqManager(HgRepository hgRepo) { | |
59 repo = hgRepo; | |
60 } | |
61 | |
62 /** | |
63 * Updates manager with up-to-date state of the mercurial queues. | |
64 */ | |
65 public void refresh() throws HgInvalidControlFileException { | |
66 applied = allKnown = Collections.emptyList(); | |
67 queueNames = Collections.emptyList(); | |
68 File repoDir = HgInternals.getRepositoryDir(repo); | |
69 final LogFacility log = HgInternals.getContext(repo).getLog(); | |
70 try { | |
71 File queues = new File(repoDir, "patches.queues"); | |
72 if (queues.isFile()) { | |
73 LineReader lr = new LineReader(queues, log).trimLines(true).skipEmpty(true); | |
74 lr.read(new SimpleLineCollector(), queueNames = new LinkedList<String>()); | |
75 } | |
76 final String queueLocation; // path under .hg to patch queue information (status, series and diff files) | |
77 File activeQueueFile = new File(repoDir, "patches.queue"); | |
78 // file is there only if it's not default queue ('patches') that is active | |
79 if (activeQueueFile.isFile()) { | |
80 ArrayList<String> contents = new ArrayList<String>(); | |
81 new LineReader(activeQueueFile, log).read(new SimpleLineCollector(), contents); | |
82 if (contents.isEmpty()) { | |
83 log.dump(getClass(), Warn, "File %s with active queue name is empty", activeQueueFile.getName()); | |
84 activeQueue = PATCHES_DIR; | |
85 queueLocation = PATCHES_DIR + '/'; | |
86 } else { | |
87 activeQueue = contents.get(0); | |
88 queueLocation = PATCHES_DIR + '-' + activeQueue + '/'; | |
89 } | |
90 } else { | |
91 activeQueue = PATCHES_DIR; | |
92 queueLocation = PATCHES_DIR + '/'; | |
93 } | |
94 final Path.Source patchLocation = new Path.Source() { | |
95 | |
96 public Path path(CharSequence p) { | |
97 StringBuilder sb = new StringBuilder(64); | |
98 sb.append(".hg/"); | |
99 sb.append(queueLocation); | |
100 sb.append(p); | |
101 return Path.create(sb); | |
102 } | |
103 }; | |
104 final File fileStatus = new File(repoDir, queueLocation + "status"); | |
105 final File fileSeries = new File(repoDir, queueLocation + "series"); | |
106 if (fileStatus.isFile()) { | |
107 new LineReader(fileStatus, log).read(new LineConsumer<List<PatchRecord>>() { | |
108 | |
109 public boolean consume(String line, List<PatchRecord> result) throws IOException { | |
110 int sep = line.indexOf(':'); | |
111 if (sep == -1) { | |
112 log.dump(MqManager.class, Warn, "Bad line in %s:%s", fileStatus.getPath(), line); | |
113 return true; | |
114 } | |
115 Nodeid nid = Nodeid.fromAscii(line.substring(0, sep)); | |
116 String name = new String(line.substring(sep+1)); | |
117 result.add(new PatchRecord(nid, name, patchLocation.path(name))); | |
118 return true; | |
119 } | |
120 }, applied = new LinkedList<PatchRecord>()); | |
121 } | |
122 if (fileSeries.isFile()) { | |
123 final Map<String,PatchRecord> name2patch = new HashMap<String, PatchRecord>(); | |
124 for (PatchRecord pr : applied) { | |
125 name2patch.put(pr.getName(), pr); | |
126 } | |
127 LinkedList<String> knownPatchNames = new LinkedList<String>(); | |
128 new LineReader(fileSeries, log).read(new SimpleLineCollector(), knownPatchNames); | |
129 // XXX read other queues? | |
130 allKnown = new ArrayList<PatchRecord>(knownPatchNames.size()); | |
131 for (String name : knownPatchNames) { | |
132 PatchRecord pr = name2patch.get(name); | |
133 if (pr == null) { | |
134 pr = new PatchRecord(null, name, patchLocation.path(name)); | |
135 } | |
136 allKnown.add(pr); | |
137 } | |
138 } | |
139 } catch (HgInvalidFileException ex) { | |
140 HgInvalidControlFileException th = new HgInvalidControlFileException(ex.getMessage(), ex.getCause(), ex.getFile()); | |
141 th.setStackTrace(ex.getStackTrace()); | |
142 throw th; | |
143 } | |
144 } | |
145 | |
146 static class SimpleLineCollector implements LineConsumer<Collection<String>> { | |
147 | |
148 public boolean consume(String line, Collection<String> result) throws IOException { | |
149 result.add(line); | |
150 return true; | |
151 } | |
152 } | |
153 | |
154 /** | |
155 * Number of patches not yet applied | |
156 * @return positive value when there are | |
157 */ | |
158 public int getQueueSize() { | |
159 return getAllKnownPatches().size() - getAppliedPatches().size(); | |
160 } | |
161 | |
162 /** | |
163 * Subset of the patches from the queue that were already applied to the repository | |
164 * <p>Analog of 'hg qapplied' | |
165 * | |
166 * <p>Clients shall call {@link #refresh()} prior to first use | |
167 * @return collection of records in no particular order, may be empty if none applied | |
168 */ | |
169 public List<PatchRecord> getAppliedPatches() { | |
170 return Collections.unmodifiableList(applied); | |
171 } | |
172 | |
173 /** | |
174 * All of the patches in the active queue that MQ knows about for this repository | |
175 * | |
176 * <p>Clients shall call {@link #refresh()} prior to first use | |
177 * @return collection of records in no particular order, may be empty if there are no patches in the queue | |
178 */ | |
179 public List<PatchRecord> getAllKnownPatches() { | |
180 return Collections.unmodifiableList(allKnown); | |
181 } | |
182 | |
183 /** | |
184 * Name of the patch queue <code>hg qqueue --active</code> which is active now. | |
185 * @return patch queue name | |
186 */ | |
187 public String getActiveQueueName() { | |
188 return activeQueue; | |
189 } | |
190 | |
191 /** | |
192 * Patch queues known in the repository, <code>hg qqueue -l</code> analog. | |
193 * There's at least one patch queue (default one names 'patches'). Only one patch queue at a time is active. | |
194 * | |
195 * @return names of patch queues | |
196 */ | |
197 public List<String> getQueueNames() { | |
198 return Collections.unmodifiableList(queueNames); | |
199 } | |
200 | |
201 public class PatchRecord { | |
202 private final Nodeid nodeid; | |
203 private final String name; | |
204 private final Path location; | |
205 | |
206 // hashCode/equals might be useful if cons becomes public | |
207 | |
208 PatchRecord(Nodeid revision, String name, Path diffLocation) { | |
209 nodeid = revision; | |
210 this.name = name; | |
211 this.location = diffLocation; | |
212 } | |
213 | |
214 /** | |
215 * Identifies changeset of the patch that has been applied to the repository | |
216 * | |
217 * @return changeset revision or <code>null</code> if this patch is not yet applied | |
218 */ | |
219 public Nodeid getRevision() { | |
220 return nodeid; | |
221 } | |
222 | |
223 /** | |
224 * Identifies patch, either based on a user-supplied name (<code>hg qnew <i>patch-name</i></code>) or | |
225 * an automatically generated name (like <code><i>revisionIndex</i>.diff</code> for imported changesets). | |
226 * Clients shall not rely on this naming scheme, though. | |
227 * | |
228 * @return never <code>null</code> | |
229 */ | |
230 public String getName() { | |
231 return name; | |
232 } | |
233 | |
234 /** | |
235 * Location of diff file with the patch, relative to repository root | |
236 * @return path to the patch, never <code>null</code> | |
237 */ | |
238 public Path getPatchLocation() { | |
239 return location; | |
240 } | |
241 } | |
242 | |
243 // TODO refine API and extract into separate classes | |
244 | |
245 interface LineConsumer<T> { | |
246 // boolean begin(File f, T paramObj) throws IOException; | |
247 boolean consume(String line, T paramObj) throws IOException; | |
248 // boolean end(File f, T paramObj) throws IOException; | |
249 } | |
250 | |
251 class LineReader { | |
252 | |
253 private final File file; | |
254 private final LogFacility log; | |
255 private boolean trimLines = true; | |
256 private boolean skipEmpty = true; | |
257 private String ignoreThatStars = null; | |
258 | |
259 LineReader(File f, LogFacility logFacility) { | |
260 file = f; | |
261 log = logFacility; | |
262 } | |
263 | |
264 /** | |
265 * default: <code>true</code> | |
266 * <code>false</code> to return line as is | |
267 */ | |
268 LineReader trimLines(boolean trim) { | |
269 trimLines = trim; | |
270 return this; | |
271 } | |
272 | |
273 /** | |
274 * default: <code>true</code> | |
275 * <code>false</code> to pass empty lines to consumer | |
276 */ | |
277 LineReader skipEmpty(boolean skip) { | |
278 skipEmpty = skip; | |
279 return this; | |
280 } | |
281 | |
282 /** | |
283 * default: doesn't skip any line. | |
284 * set e.g. to "#" or "//" to skip lines that start with such prefix | |
285 */ | |
286 LineReader ignoreLineComments(String lineStart) { | |
287 ignoreThatStars = lineStart; | |
288 return this; | |
289 } | |
290 | |
291 <T> void read(LineConsumer<T> consumer, T paramObj) throws HgInvalidFileException { | |
292 BufferedReader statusFileReader = null; | |
293 try { | |
294 // consumer.begin(file, paramObj); | |
295 statusFileReader = new BufferedReader(new FileReader(file)); | |
296 String line; | |
297 boolean ok = true; | |
298 while (ok && (line = statusFileReader.readLine()) != null) { | |
299 if (trimLines) { | |
300 line = line.trim(); | |
301 } | |
302 if (ignoreThatStars != null && line.startsWith(ignoreThatStars)) { | |
303 continue; | |
304 } | |
305 if (!skipEmpty || line.length() > 0) { | |
306 ok = consumer.consume(line, paramObj); | |
307 } | |
308 } | |
309 } catch (IOException ex) { | |
310 throw new HgInvalidFileException(ex.getMessage(), ex, file); | |
311 } finally { | |
312 try { | |
313 statusFileReader.close(); | |
314 } catch (IOException ex) { | |
315 log.dump(MqManager.class, Warn, ex, null); | |
316 } | |
317 // try { | |
318 // consumer.end(file, paramObj); | |
319 // } catch (IOException ex) { | |
320 // log.warn(MqManager.class, ex, null); | |
321 // } | |
322 } | |
323 } | |
324 } | |
325 } |