Mercurial > jhg
comparison src/org/tmatesoft/hg/repo/HgBundle.java @ 182:f26ffe04ced0
Refactor HgBundle to dispatch changes found through callback
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 12 Apr 2011 19:36:18 +0200 |
parents | 71ddbf8603e8 |
children | 44a34baabea0 |
comparison
equal
deleted
inserted
replaced
181:cd3371670f0b | 182:f26ffe04ced0 |
---|---|
73 } | 73 } |
74 } | 74 } |
75 return da; | 75 return da; |
76 } | 76 } |
77 | 77 |
78 // shows changes recorded in the bundle that are missing from the supplied repository | 78 /** |
79 public void changes(final HgRepository hgRepo) throws HgException, IOException { | 79 * Get changes recorded in the bundle that are missing from the supplied repository. |
80 Inspector insp = new Inspector() { | 80 * @param hgRepo repository that shall possess base revision for this bundle |
81 * @param inspector callback to get each changeset found | |
82 */ | |
83 public void changes(final HgRepository hgRepo, final HgChangelog.Inspector inspector) throws HgException, IOException { | |
84 Inspector bundleInsp = new Inspector() { | |
81 DigestHelper dh = new DigestHelper(); | 85 DigestHelper dh = new DigestHelper(); |
82 boolean emptyChangelog = true; | 86 boolean emptyChangelog = true; |
83 private DataAccess prevRevContent; | 87 private DataAccess prevRevContent; |
88 private int revisionIndex; | |
84 | 89 |
85 public void changelogStart() { | 90 public void changelogStart() { |
86 emptyChangelog = true; | 91 emptyChangelog = true; |
87 | 92 revisionIndex = 0; |
88 } | 93 } |
89 | 94 |
90 public void changelogEnd() { | 95 public void changelogEnd() { |
91 if (emptyChangelog) { | 96 if (emptyChangelog) { |
92 throw new IllegalStateException("No changelog group in the bundle"); // XXX perhaps, just be silent and/or log? | 97 throw new IllegalStateException("No changelog group in the bundle"); // XXX perhaps, just be silent and/or log? |
139 dh = dh.sha1(ge.firstParent(), ge.secondParent(), csetContent); // XXX ge may give me access to byte[] content of nodeid directly, perhaps, I don't need DH to be friend of Nodeid? | 144 dh = dh.sha1(ge.firstParent(), ge.secondParent(), csetContent); // XXX ge may give me access to byte[] content of nodeid directly, perhaps, I don't need DH to be friend of Nodeid? |
140 if (!ge.node().equalsTo(dh.asBinary())) { | 145 if (!ge.node().equalsTo(dh.asBinary())) { |
141 throw new IllegalStateException("Integrity check failed on " + bundleFile + ", node:" + ge.node()); | 146 throw new IllegalStateException("Integrity check failed on " + bundleFile + ", node:" + ge.node()); |
142 } | 147 } |
143 ByteArrayDataAccess csetDataAccess = new ByteArrayDataAccess(csetContent); | 148 ByteArrayDataAccess csetDataAccess = new ByteArrayDataAccess(csetContent); |
144 if (changelog.isKnown(ge.node())) { | |
145 System.out.print("+"); | |
146 } else { | |
147 System.out.print("-"); | |
148 } | |
149 RawChangeset cs = RawChangeset.parse(csetDataAccess); | 149 RawChangeset cs = RawChangeset.parse(csetDataAccess); |
150 System.out.println(cs.toString()); | 150 inspector.next(revisionIndex++, ge.node(), cs); |
151 prevRevContent.done(); | 151 prevRevContent.done(); |
152 prevRevContent = csetDataAccess.reset(); | 152 prevRevContent = csetDataAccess.reset(); |
153 } catch (CancelledException ex) { | 153 } catch (CancelledException ex) { |
154 return false; | 154 return false; |
155 } catch (Exception ex) { | 155 } catch (Exception ex) { |
162 public void manifestEnd() {} | 162 public void manifestEnd() {} |
163 public void fileStart(String name) {} | 163 public void fileStart(String name) {} |
164 public void fileEnd(String name) {} | 164 public void fileEnd(String name) {} |
165 | 165 |
166 }; | 166 }; |
167 inspectChangelog(insp); | 167 inspectChangelog(bundleInsp); |
168 } | 168 } |
169 | 169 |
170 public void dump() throws IOException { | 170 public void dump() throws IOException { |
171 Dump dump = new Dump(); | 171 Dump dump = new Dump(); |
172 inspectAll(dump); | 172 inspectAll(dump); |
237 if (inspector == null) { | 237 if (inspector == null) { |
238 throw new IllegalArgumentException(); | 238 throw new IllegalArgumentException(); |
239 } | 239 } |
240 DataAccess da = getDataStream(); | 240 DataAccess da = getDataStream(); |
241 try { | 241 try { |
242 if (da.isEmpty()) { | 242 internalInspectChangelog(da, inspector); |
243 return; | |
244 } | |
245 inspector.changelogStart(); | |
246 readGroup(da, inspector); | |
247 inspector.changelogEnd(); | |
248 } finally { | 243 } finally { |
249 da.done(); | 244 da.done(); |
250 } | 245 } |
251 } | 246 } |
252 | 247 |
258 try { | 253 try { |
259 if (da.isEmpty()) { | 254 if (da.isEmpty()) { |
260 return; | 255 return; |
261 } | 256 } |
262 skipGroup(da); // changelog | 257 skipGroup(da); // changelog |
263 if (!da.isEmpty()) { | 258 internalInspectManifest(da, inspector); |
264 inspector.manifestStart(); | |
265 readGroup(da, inspector); | |
266 inspector.manifestEnd(); | |
267 } | |
268 } finally { | 259 } finally { |
269 da.done(); | 260 da.done(); |
270 } | 261 } |
271 } | 262 } |
272 | 263 |
273 public void inspectFiles(Inspector inspector) throws IOException { | 264 public void inspectFiles(Inspector inspector) throws IOException { |
274 if (inspector == null) { | |
275 throw new IllegalArgumentException(); | |
276 } | |
277 DataAccess da = getDataStream(); | |
278 try { | |
279 if (!da.isEmpty()) { | |
280 skipGroup(da); // changelog | |
281 } | |
282 if (!da.isEmpty()) { | |
283 skipGroup(da); // manifest | |
284 } | |
285 while (!da.isEmpty()) { | |
286 int fnameLen = da.readInt(); | |
287 if (fnameLen <= 4) { | |
288 break; // null chunk, the last one. | |
289 } | |
290 byte[] nameBuf = new byte[fnameLen - 4]; | |
291 da.readBytes(nameBuf, 0, nameBuf.length); | |
292 String fname = new String(nameBuf); | |
293 inspector.fileStart(fname); | |
294 readGroup(da, inspector); | |
295 inspector.fileEnd(fname); | |
296 } | |
297 } finally { | |
298 da.done(); | |
299 } | |
300 } | |
301 | |
302 public void inspectAll(Inspector inspector) throws IOException { | |
303 if (inspector == null) { | 265 if (inspector == null) { |
304 throw new IllegalArgumentException(); | 266 throw new IllegalArgumentException(); |
305 } | 267 } |
306 DataAccess da = getDataStream(); | 268 DataAccess da = getDataStream(); |
307 try { | 269 try { |
308 if (da.isEmpty()) { | 270 if (da.isEmpty()) { |
309 return; | 271 return; |
310 } | 272 } |
311 inspector.changelogStart(); | 273 skipGroup(da); // changelog |
312 readGroup(da, inspector); | |
313 inspector.changelogEnd(); | |
314 // | |
315 if (da.isEmpty()) { | 274 if (da.isEmpty()) { |
316 return; | 275 return; |
317 } | 276 } |
318 inspector.manifestStart(); | 277 skipGroup(da); // manifest |
319 readGroup(da, inspector); | 278 internalInspectFiles(da, inspector); |
320 inspector.manifestEnd(); | |
321 // | |
322 while (!da.isEmpty()) { | |
323 int fnameLen = da.readInt(); | |
324 if (fnameLen <= 4) { | |
325 break; // null chunk, the last one. | |
326 } | |
327 byte[] fnameBuf = new byte[fnameLen - 4]; | |
328 da.readBytes(fnameBuf, 0, fnameBuf.length); | |
329 String name = new String(fnameBuf); | |
330 inspector.fileStart(name); | |
331 readGroup(da, inspector); | |
332 inspector.fileEnd(name); | |
333 } | |
334 } finally { | 279 } finally { |
335 da.done(); | 280 da.done(); |
281 } | |
282 } | |
283 | |
284 public void inspectAll(Inspector inspector) throws IOException { | |
285 if (inspector == null) { | |
286 throw new IllegalArgumentException(); | |
287 } | |
288 DataAccess da = getDataStream(); | |
289 try { | |
290 internalInspectChangelog(da, inspector); | |
291 internalInspectManifest(da, inspector); | |
292 internalInspectFiles(da, inspector); | |
293 } finally { | |
294 da.done(); | |
295 } | |
296 } | |
297 | |
298 private void internalInspectChangelog(DataAccess da, Inspector inspector) throws IOException { | |
299 if (da.isEmpty()) { | |
300 return; | |
301 } | |
302 inspector.changelogStart(); | |
303 readGroup(da, inspector); | |
304 inspector.changelogEnd(); | |
305 } | |
306 | |
307 private void internalInspectManifest(DataAccess da, Inspector inspector) throws IOException { | |
308 if (da.isEmpty()) { | |
309 return; | |
310 } | |
311 inspector.manifestStart(); | |
312 readGroup(da, inspector); | |
313 inspector.manifestEnd(); | |
314 } | |
315 | |
316 private void internalInspectFiles(DataAccess da, Inspector inspector) throws IOException { | |
317 while (!da.isEmpty()) { | |
318 int fnameLen = da.readInt(); | |
319 if (fnameLen <= 4) { | |
320 break; // null chunk, the last one. | |
321 } | |
322 byte[] fnameBuf = new byte[fnameLen - 4]; | |
323 da.readBytes(fnameBuf, 0, fnameBuf.length); | |
324 String name = new String(fnameBuf); | |
325 inspector.fileStart(name); | |
326 readGroup(da, inspector); | |
327 inspector.fileEnd(name); | |
336 } | 328 } |
337 } | 329 } |
338 | 330 |
339 private static void readGroup(DataAccess da, Inspector inspector) throws IOException { | 331 private static void readGroup(DataAccess da, Inspector inspector) throws IOException { |
340 int len = da.readInt(); | 332 int len = da.readInt(); |