comparison src/org/tmatesoft/hg/repo/HgBundle.java @ 667:fba85bc1dfb8

Refactoring: move all encoding/decoding operations into single place, EncodingHelper
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 11 Jul 2013 17:54:08 +0200
parents 46b56864b483
children 545b1d4cc11d
comparison
equal deleted inserted replaced
666:27a3ddedd6cc 667:fba85bc1dfb8
29 import org.tmatesoft.hg.internal.Callback; 29 import org.tmatesoft.hg.internal.Callback;
30 import org.tmatesoft.hg.internal.DataAccess; 30 import org.tmatesoft.hg.internal.DataAccess;
31 import org.tmatesoft.hg.internal.DataAccessProvider; 31 import org.tmatesoft.hg.internal.DataAccessProvider;
32 import org.tmatesoft.hg.internal.DataSerializer; 32 import org.tmatesoft.hg.internal.DataSerializer;
33 import org.tmatesoft.hg.internal.DigestHelper; 33 import org.tmatesoft.hg.internal.DigestHelper;
34 import org.tmatesoft.hg.internal.EncodingHelper;
34 import org.tmatesoft.hg.internal.Experimental; 35 import org.tmatesoft.hg.internal.Experimental;
35 import org.tmatesoft.hg.internal.FileUtils; 36 import org.tmatesoft.hg.internal.FileUtils;
36 import org.tmatesoft.hg.internal.InflaterDataAccess; 37 import org.tmatesoft.hg.internal.InflaterDataAccess;
37 import org.tmatesoft.hg.internal.Internals; 38 import org.tmatesoft.hg.internal.Internals;
38 import org.tmatesoft.hg.internal.Lifecycle; 39 import org.tmatesoft.hg.internal.Lifecycle;
39 import org.tmatesoft.hg.internal.Patch; 40 import org.tmatesoft.hg.internal.Patch;
41 import org.tmatesoft.hg.repo.HgChangelog.ChangesetParser;
40 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; 42 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
41 import org.tmatesoft.hg.util.Adaptable; 43 import org.tmatesoft.hg.util.Adaptable;
42 import org.tmatesoft.hg.util.CancelledException; 44 import org.tmatesoft.hg.util.CancelledException;
43 45
44 /** 46 /**
53 public class HgBundle { 55 public class HgBundle {
54 56
55 private final File bundleFile; 57 private final File bundleFile;
56 private final DataAccessProvider accessProvider; 58 private final DataAccessProvider accessProvider;
57 private final SessionContext ctx; 59 private final SessionContext ctx;
60 private final EncodingHelper fnDecorer;
58 private Lifecycle.BasicCallback flowControl; 61 private Lifecycle.BasicCallback flowControl;
59 62
60 HgBundle(SessionContext sessionContext, DataAccessProvider dap, File bundle) { 63 HgBundle(SessionContext sessionContext, DataAccessProvider dap, File bundle) {
61 ctx = sessionContext; 64 ctx = sessionContext;
62 accessProvider = dap; 65 accessProvider = dap;
63 bundleFile = bundle; 66 bundleFile = bundle;
67 fnDecorer = Internals.buildFileNameEncodingHelper(new SessionContext.SourcePrim(ctx));
64 } 68 }
65 69
66 private DataAccess getDataStream() throws IOException { 70 private DataAccess getDataStream() throws IOException {
67 DataAccess da = accessProvider.createReader(bundleFile, false); 71 DataAccess da = accessProvider.createReader(bundleFile, false);
68 byte[] signature = new byte[6]; 72 byte[] signature = new byte[6];
110 Inspector bundleInsp = new Inspector() { 114 Inspector bundleInsp = new Inspector() {
111 DigestHelper dh = new DigestHelper(); 115 DigestHelper dh = new DigestHelper();
112 boolean emptyChangelog = true; 116 boolean emptyChangelog = true;
113 private DataAccess prevRevContent; 117 private DataAccess prevRevContent;
114 private int revisionIndex; 118 private int revisionIndex;
119 private ChangesetParser csetBuilder;
115 120
116 public void changelogStart() { 121 public void changelogStart() {
117 emptyChangelog = true; 122 emptyChangelog = true;
118 revisionIndex = 0; 123 revisionIndex = 0;
124 csetBuilder = new ChangesetParser(hgRepo, true);
119 } 125 }
120 126
121 public void changelogEnd() { 127 public void changelogEnd() {
122 if (emptyChangelog) { 128 if (emptyChangelog) {
123 throw new IllegalStateException("No changelog group in the bundle"); // XXX perhaps, just be silent and/or log? 129 throw new IllegalStateException("No changelog group in the bundle"); // XXX perhaps, just be silent and/or log?
170 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? 176 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?
171 if (!ge.node().equalsTo(dh.asBinary())) { 177 if (!ge.node().equalsTo(dh.asBinary())) {
172 throw new HgInvalidStateException(String.format("Integrity check failed on %s, node: %s", bundleFile, ge.node().shortNotation())); 178 throw new HgInvalidStateException(String.format("Integrity check failed on %s, node: %s", bundleFile, ge.node().shortNotation()));
173 } 179 }
174 ByteArrayDataAccess csetDataAccess = new ByteArrayDataAccess(csetContent); 180 ByteArrayDataAccess csetDataAccess = new ByteArrayDataAccess(csetContent);
175 RawChangeset cs = RawChangeset.parse(csetDataAccess); 181 RawChangeset cs = csetBuilder.parse(csetDataAccess);
176 inspector.next(revisionIndex++, ge.node(), cs); 182 inspector.next(revisionIndex++, ge.node(), cs);
177 prevRevContent.done(); 183 prevRevContent.done();
178 prevRevContent = csetDataAccess.reset(); 184 prevRevContent = csetDataAccess.reset();
179 } catch (CancelledException ex) { 185 } catch (CancelledException ex) {
180 return false; 186 return false;
395 if (fnameLen <= 4) { 401 if (fnameLen <= 4) {
396 break; // null chunk, the last one. 402 break; // null chunk, the last one.
397 } 403 }
398 byte[] fnameBuf = new byte[fnameLen - 4]; 404 byte[] fnameBuf = new byte[fnameLen - 4];
399 da.readBytes(fnameBuf, 0, fnameBuf.length); 405 da.readBytes(fnameBuf, 0, fnameBuf.length);
400 String name = new String(fnameBuf); 406 String name = fnDecorer.fromBundle(fnameBuf, 0, fnameBuf.length);
401 inspector.fileStart(name); 407 inspector.fileStart(name);
402 if (flowControl.isStopped()) { 408 if (flowControl.isStopped()) {
403 return; 409 return;
404 } 410 }
405 readGroup(da, inspector); 411 readGroup(da, inspector);