tikhomirov@36: /*
tikhomirov@74: * Copyright (c) 2011 TMate Software Ltd
tikhomirov@74: *
tikhomirov@74: * This program is free software; you can redistribute it and/or modify
tikhomirov@74: * it under the terms of the GNU General Public License as published by
tikhomirov@74: * the Free Software Foundation; version 2 of the License.
tikhomirov@74: *
tikhomirov@74: * This program is distributed in the hope that it will be useful,
tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@74: * GNU General Public License for more details.
tikhomirov@74: *
tikhomirov@74: * For information on how to redistribute this software under
tikhomirov@74: * the terms of a license other than GNU General Public License
tikhomirov@102: * contact TMate Software at support@hg4j.com
tikhomirov@36: */
tikhomirov@74: package org.tmatesoft.hg.repo;
tikhomirov@36:
tikhomirov@169: import static org.tmatesoft.hg.core.Nodeid.NULL;
tikhomirov@169:
tikhomirov@36: import java.io.File;
tikhomirov@36: import java.io.IOException;
tikhomirov@36: import java.util.LinkedList;
tikhomirov@36: import java.util.List;
tikhomirov@36:
tikhomirov@169: import org.tmatesoft.hg.core.HgBadStateException;
tikhomirov@157: import org.tmatesoft.hg.core.HgException;
tikhomirov@74: import org.tmatesoft.hg.core.Nodeid;
tikhomirov@157: import org.tmatesoft.hg.internal.ByteArrayChannel;
tikhomirov@157: import org.tmatesoft.hg.internal.ByteArrayDataAccess;
tikhomirov@74: import org.tmatesoft.hg.internal.DataAccess;
tikhomirov@74: import org.tmatesoft.hg.internal.DataAccessProvider;
tikhomirov@74: import org.tmatesoft.hg.internal.DigestHelper;
tikhomirov@169: import org.tmatesoft.hg.internal.InflaterDataAccess;
tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream;
tikhomirov@154: import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
tikhomirov@157: import org.tmatesoft.hg.util.CancelledException;
tikhomirov@74:
tikhomirov@36: /**
tikhomirov@36: * @see http://mercurial.selenic.com/wiki/BundleFormat
tikhomirov@169: *
tikhomirov@74: * @author Artem Tikhomirov
tikhomirov@74: * @author TMate Software Ltd.
tikhomirov@36: */
tikhomirov@36: public class HgBundle {
tikhomirov@36:
tikhomirov@36: private final File bundleFile;
tikhomirov@36: private final DataAccessProvider accessProvider;
tikhomirov@36:
tikhomirov@169: HgBundle(DataAccessProvider dap, File bundle) {
tikhomirov@36: accessProvider = dap;
tikhomirov@36: bundleFile = bundle;
tikhomirov@36: }
tikhomirov@36:
tikhomirov@169: private DataAccess getDataStream() throws IOException {
tikhomirov@37: DataAccess da = accessProvider.create(bundleFile);
tikhomirov@169: byte[] signature = new byte[6];
tikhomirov@169: if (da.length() > 6) {
tikhomirov@169: da.readBytes(signature, 0, 6);
tikhomirov@169: if (signature[0] == 'H' && signature[1] == 'G' && signature[2] == '1' && signature[3] == '0') {
tikhomirov@169: if (signature[4] == 'G' && signature[5] == 'Z') {
tikhomirov@169: return new InflaterDataAccess(da, 6, da.length() - 6);
tikhomirov@169: }
tikhomirov@169: if (signature[4] == 'B' && signature[5] == 'Z') {
tikhomirov@169: throw HgRepository.notImplemented();
tikhomirov@169: }
tikhomirov@169: if (signature[4] != 'U' || signature[5] != 'N') {
tikhomirov@169: throw new HgBadStateException("Bad bundle signature:" + new String(signature));
tikhomirov@169: }
tikhomirov@169: // "...UN", fall-through
tikhomirov@169: } else {
tikhomirov@169: da.reset();
tikhomirov@39: }
tikhomirov@169: }
tikhomirov@169: return da;
tikhomirov@169: }
tikhomirov@169:
tikhomirov@182: /**
tikhomirov@182: * Get changes recorded in the bundle that are missing from the supplied repository.
tikhomirov@182: * @param hgRepo repository that shall possess base revision for this bundle
tikhomirov@182: * @param inspector callback to get each changeset found
tikhomirov@182: */
tikhomirov@182: public void changes(final HgRepository hgRepo, final HgChangelog.Inspector inspector) throws HgException, IOException {
tikhomirov@182: Inspector bundleInsp = new Inspector() {
tikhomirov@169: DigestHelper dh = new DigestHelper();
tikhomirov@169: boolean emptyChangelog = true;
tikhomirov@169: private DataAccess prevRevContent;
tikhomirov@182: private int revisionIndex;
tikhomirov@169:
tikhomirov@169: public void changelogStart() {
tikhomirov@169: emptyChangelog = true;
tikhomirov@182: revisionIndex = 0;
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void changelogEnd() {
tikhomirov@169: if (emptyChangelog) {
tikhomirov@169: throw new IllegalStateException("No changelog group in the bundle"); // XXX perhaps, just be silent and/or log?
tikhomirov@42: }
tikhomirov@37: }
tikhomirov@169:
tikhomirov@169: /*
tikhomirov@169: * Despite that BundleFormat wiki says: "Each Changelog entry patches the result of all previous patches
tikhomirov@169: * (the previous, or parent patch of a given patch p is the patch that has a node equal to p's p1 field)",
tikhomirov@169: * it seems not to hold true. Instead, each entry patches previous one, regardless of whether the one
tikhomirov@169: * before is its parent (i.e. ge.firstParent()) or not.
tikhomirov@169: *
tikhomirov@169: Actual state in the changelog.i
tikhomirov@169: Index Offset Flags Packed Actual Base Rev Link Rev Parent1 Parent2 nodeid
tikhomirov@169: 50: 9212 0 209 329 48 50 49 -1 f1db8610da62a3e0beb8d360556ee1fd6eb9885e
tikhomirov@169: 51: 9421 0 278 688 48 51 50 -1 9429c7bd1920fab164a9d2b621d38d57bcb49ae0
tikhomirov@169: 52: 9699 0 154 179 52 52 50 -1 30bd389788464287cee22ccff54c330a4b715de5
tikhomirov@169: 53: 9853 0 133 204 52 53 51 52 a6f39e595b2b54f56304470269a936ead77f5725
tikhomirov@169: 54: 9986 0 156 182 54 54 52 -1 fd4f2c98995beb051070630c272a9be87bef617d
tikhomirov@169:
tikhomirov@169: Excerpt from bundle (nodeid, p1, p2, cs):
tikhomirov@169: f1db8610da62a3e0beb8d360556ee1fd6eb9885e 26e3eeaa39623de552b45ee1f55c14f36460f220 0000000000000000000000000000000000000000 f1db8610da62a3e0beb8d360556ee1fd6eb9885e; patches:4
tikhomirov@169: 9429c7bd1920fab164a9d2b621d38d57bcb49ae0 f1db8610da62a3e0beb8d360556ee1fd6eb9885e 0000000000000000000000000000000000000000 9429c7bd1920fab164a9d2b621d38d57bcb49ae0; patches:3
tikhomirov@169: > 30bd389788464287cee22ccff54c330a4b715de5 f1db8610da62a3e0beb8d360556ee1fd6eb9885e 0000000000000000000000000000000000000000 30bd389788464287cee22ccff54c330a4b715de5; patches:3
tikhomirov@169: a6f39e595b2b54f56304470269a936ead77f5725 9429c7bd1920fab164a9d2b621d38d57bcb49ae0 30bd389788464287cee22ccff54c330a4b715de5 a6f39e595b2b54f56304470269a936ead77f5725; patches:3
tikhomirov@169: fd4f2c98995beb051070630c272a9be87bef617d 30bd389788464287cee22ccff54c330a4b715de5 0000000000000000000000000000000000000000 fd4f2c98995beb051070630c272a9be87bef617d; patches:3
tikhomirov@169:
tikhomirov@169: To recreate 30bd..e5, one have to take content of 9429..e0, not its p1 f1db..5e
tikhomirov@169: */
tikhomirov@169: public boolean element(GroupElement ge) {
tikhomirov@169: emptyChangelog = false;
tikhomirov@169: HgChangelog changelog = hgRepo.getChangelog();
tikhomirov@169: try {
tikhomirov@169: if (prevRevContent == null) {
tikhomirov@169: if (NULL.equals(ge.firstParent()) && NULL.equals(ge.secondParent())) {
tikhomirov@169: prevRevContent = new ByteArrayDataAccess(new byte[0]);
tikhomirov@169: } else {
tikhomirov@169: final Nodeid base = ge.firstParent();
tikhomirov@169: if (!changelog.isKnown(base) /*only first parent, that's Bundle contract*/) {
tikhomirov@169: throw new IllegalStateException(String.format("Revision %s needs a parent %s, which is missing in the supplied repo %s", ge.node().shortNotation(), base.shortNotation(), hgRepo.toString()));
tikhomirov@169: }
tikhomirov@169: ByteArrayChannel bac = new ByteArrayChannel();
tikhomirov@169: changelog.rawContent(base, bac); // FIXME get DataAccess directly, to avoid
tikhomirov@169: // extra byte[] (inside ByteArrayChannel) duplication just for the sake of subsequent ByteArrayDataChannel wrap.
tikhomirov@169: prevRevContent = new ByteArrayDataAccess(bac.toArray());
tikhomirov@169: }
tikhomirov@169: }
tikhomirov@169: //
tikhomirov@169: byte[] csetContent = ge.apply(prevRevContent);
tikhomirov@169: 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?
tikhomirov@169: if (!ge.node().equalsTo(dh.asBinary())) {
tikhomirov@169: throw new IllegalStateException("Integrity check failed on " + bundleFile + ", node:" + ge.node());
tikhomirov@169: }
tikhomirov@169: ByteArrayDataAccess csetDataAccess = new ByteArrayDataAccess(csetContent);
tikhomirov@169: RawChangeset cs = RawChangeset.parse(csetDataAccess);
tikhomirov@182: inspector.next(revisionIndex++, ge.node(), cs);
tikhomirov@170: prevRevContent.done();
tikhomirov@169: prevRevContent = csetDataAccess.reset();
tikhomirov@169: } catch (CancelledException ex) {
tikhomirov@169: return false;
tikhomirov@169: } catch (Exception ex) {
tikhomirov@169: throw new HgBadStateException(ex); // FIXME
tikhomirov@169: }
tikhomirov@169: return true;
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void manifestStart() {}
tikhomirov@169: public void manifestEnd() {}
tikhomirov@169: public void fileStart(String name) {}
tikhomirov@169: public void fileEnd(String name) {}
tikhomirov@169:
tikhomirov@169: };
tikhomirov@182: inspectChangelog(bundleInsp);
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void dump() throws IOException {
tikhomirov@169: Dump dump = new Dump();
tikhomirov@169: inspectAll(dump);
tikhomirov@169: System.out.println("Total files:" + dump.names.size());
tikhomirov@169: for (String s : dump.names) {
tikhomirov@169: System.out.println(s);
tikhomirov@169: }
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: // callback to minimize amount of Strings and Nodeids instantiated
tikhomirov@169: public interface Inspector {
tikhomirov@169: void changelogStart();
tikhomirov@169:
tikhomirov@169: void changelogEnd();
tikhomirov@169:
tikhomirov@169: void manifestStart();
tikhomirov@169:
tikhomirov@169: void manifestEnd();
tikhomirov@169:
tikhomirov@169: void fileStart(String name);
tikhomirov@169:
tikhomirov@169: void fileEnd(String name);
tikhomirov@169:
tikhomirov@169: /**
tikhomirov@170: * XXX desperately need exceptions here
tikhomirov@170: * @param element data element, instance might be reused, don't keep a reference to it or its raw data
tikhomirov@169: * @return true
to continue
tikhomirov@169: */
tikhomirov@169: boolean element(GroupElement element);
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public static class Dump implements Inspector {
tikhomirov@169: public final LinkedList names = new LinkedList();
tikhomirov@169:
tikhomirov@169: public void changelogStart() {
tikhomirov@169: System.out.println("Changelog group");
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void changelogEnd() {
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void manifestStart() {
tikhomirov@169: System.out.println("Manifest group");
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void manifestEnd() {
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void fileStart(String name) {
tikhomirov@169: names.add(name);
tikhomirov@169: System.out.println(name);
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void fileEnd(String name) {
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public boolean element(GroupElement ge) {
tikhomirov@169: try {
tikhomirov@169: System.out.printf(" %s %s %s %s; patches:%d\n", ge.node(), ge.firstParent(), ge.secondParent(), ge.cset(), ge.patches().size());
tikhomirov@169: } catch (Exception ex) {
tikhomirov@169: ex.printStackTrace(); // FIXME
tikhomirov@169: }
tikhomirov@169: return true;
tikhomirov@169: }
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void inspectChangelog(Inspector inspector) throws IOException {
tikhomirov@169: if (inspector == null) {
tikhomirov@169: throw new IllegalArgumentException();
tikhomirov@169: }
tikhomirov@169: DataAccess da = getDataStream();
tikhomirov@169: try {
tikhomirov@182: internalInspectChangelog(da, inspector);
tikhomirov@37: } finally {
tikhomirov@37: da.done();
tikhomirov@37: }
tikhomirov@37: }
tikhomirov@37:
tikhomirov@169: public void inspectManifest(Inspector inspector) throws IOException {
tikhomirov@169: if (inspector == null) {
tikhomirov@169: throw new IllegalArgumentException();
tikhomirov@169: }
tikhomirov@169: DataAccess da = getDataStream();
tikhomirov@36: try {
tikhomirov@169: if (da.isEmpty()) {
tikhomirov@169: return;
tikhomirov@169: }
tikhomirov@169: skipGroup(da); // changelog
tikhomirov@182: internalInspectManifest(da, inspector);
tikhomirov@36: } finally {
tikhomirov@36: da.done();
tikhomirov@36: }
tikhomirov@36: }
tikhomirov@36:
tikhomirov@169: public void inspectFiles(Inspector inspector) throws IOException {
tikhomirov@169: if (inspector == null) {
tikhomirov@169: throw new IllegalArgumentException();
tikhomirov@169: }
tikhomirov@169: DataAccess da = getDataStream();
tikhomirov@169: try {
tikhomirov@182: if (da.isEmpty()) {
tikhomirov@182: return;
tikhomirov@169: }
tikhomirov@182: skipGroup(da); // changelog
tikhomirov@182: if (da.isEmpty()) {
tikhomirov@182: return;
tikhomirov@169: }
tikhomirov@182: skipGroup(da); // manifest
tikhomirov@182: internalInspectFiles(da, inspector);
tikhomirov@169: } finally {
tikhomirov@169: da.done();
tikhomirov@169: }
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public void inspectAll(Inspector inspector) throws IOException {
tikhomirov@169: if (inspector == null) {
tikhomirov@169: throw new IllegalArgumentException();
tikhomirov@169: }
tikhomirov@169: DataAccess da = getDataStream();
tikhomirov@169: try {
tikhomirov@182: internalInspectChangelog(da, inspector);
tikhomirov@182: internalInspectManifest(da, inspector);
tikhomirov@182: internalInspectFiles(da, inspector);
tikhomirov@169: } finally {
tikhomirov@169: da.done();
tikhomirov@169: }
tikhomirov@169: }
tikhomirov@169:
tikhomirov@182: private void internalInspectChangelog(DataAccess da, Inspector inspector) throws IOException {
tikhomirov@182: if (da.isEmpty()) {
tikhomirov@182: return;
tikhomirov@182: }
tikhomirov@182: inspector.changelogStart();
tikhomirov@182: readGroup(da, inspector);
tikhomirov@182: inspector.changelogEnd();
tikhomirov@182: }
tikhomirov@182:
tikhomirov@182: private void internalInspectManifest(DataAccess da, Inspector inspector) throws IOException {
tikhomirov@182: if (da.isEmpty()) {
tikhomirov@182: return;
tikhomirov@182: }
tikhomirov@182: inspector.manifestStart();
tikhomirov@182: readGroup(da, inspector);
tikhomirov@182: inspector.manifestEnd();
tikhomirov@182: }
tikhomirov@182:
tikhomirov@182: private void internalInspectFiles(DataAccess da, Inspector inspector) throws IOException {
tikhomirov@182: while (!da.isEmpty()) {
tikhomirov@182: int fnameLen = da.readInt();
tikhomirov@182: if (fnameLen <= 4) {
tikhomirov@182: break; // null chunk, the last one.
tikhomirov@182: }
tikhomirov@182: byte[] fnameBuf = new byte[fnameLen - 4];
tikhomirov@182: da.readBytes(fnameBuf, 0, fnameBuf.length);
tikhomirov@182: String name = new String(fnameBuf);
tikhomirov@182: inspector.fileStart(name);
tikhomirov@182: readGroup(da, inspector);
tikhomirov@182: inspector.fileEnd(name);
tikhomirov@182: }
tikhomirov@182: }
tikhomirov@182:
tikhomirov@169: private static void readGroup(DataAccess da, Inspector inspector) throws IOException {
tikhomirov@36: int len = da.readInt();
tikhomirov@169: boolean good2go = true;
tikhomirov@169: while (len > 4 && !da.isEmpty() && good2go) {
tikhomirov@36: byte[] nb = new byte[80];
tikhomirov@36: da.readBytes(nb, 0, 80);
tikhomirov@169: int dataLength = len - 84 /* length field + 4 nodeids */;
tikhomirov@169: byte[] data = new byte[dataLength];
tikhomirov@169: da.readBytes(data, 0, dataLength);
tikhomirov@169: DataAccess slice = new ByteArrayDataAccess(data); // XXX in fact, may pass a slicing DataAccess.
tikhomirov@169: // Just need to make sure that we seek to proper location afterwards (where next GroupElement starts),
tikhomirov@169: // regardless whether that slice has read it or not.
tikhomirov@169: GroupElement ge = new GroupElement(nb, slice);
tikhomirov@169: good2go = inspector.element(ge);
tikhomirov@170: slice.done(); // BADA doesn't implement done(), but it could (e.g. free array)
tikhomirov@170: /// and we'd better tell it we are not going to use it any more. However, it's important to ensure Inspector
tikhomirov@170: // implementations out there do not retain GroupElement.rawData()
tikhomirov@36: len = da.isEmpty() ? 0 : da.readInt();
tikhomirov@36: }
tikhomirov@169: // need to skip up to group end if inspector told he don't want to continue with the group,
tikhomirov@169: // because outer code may try to read next group immediately as we return back.
tikhomirov@169: while (len > 4 && !da.isEmpty()) {
tikhomirov@169: da.skip(len - 4 /* length field */);
tikhomirov@169: len = da.isEmpty() ? 0 : da.readInt();
tikhomirov@169: }
tikhomirov@36: }
tikhomirov@36:
tikhomirov@169: private static void skipGroup(DataAccess da) throws IOException {
tikhomirov@169: int len = da.readInt();
tikhomirov@169: while (len > 4 && !da.isEmpty()) {
tikhomirov@169: da.skip(len - 4); // sizeof(int)
tikhomirov@169: len = da.isEmpty() ? 0 : da.readInt();
tikhomirov@169: }
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public static class GroupElement {
tikhomirov@169: private final byte[] header; // byte[80] takes 120 bytes, 4 Nodeids - 192
tikhomirov@169: private final DataAccess dataAccess;
tikhomirov@36: private List patches;
tikhomirov@169:
tikhomirov@169: GroupElement(byte[] fourNodeids, DataAccess rawDataAccess) {
tikhomirov@36: assert fourNodeids != null && fourNodeids.length == 80;
tikhomirov@36: header = fourNodeids;
tikhomirov@169: dataAccess = rawDataAccess;
tikhomirov@36: }
tikhomirov@169:
tikhomirov@36: public Nodeid node() {
tikhomirov@36: return Nodeid.fromBinary(header, 0);
tikhomirov@36: }
tikhomirov@169:
tikhomirov@36: public Nodeid firstParent() {
tikhomirov@36: return Nodeid.fromBinary(header, 20);
tikhomirov@36: }
tikhomirov@169:
tikhomirov@36: public Nodeid secondParent() {
tikhomirov@36: return Nodeid.fromBinary(header, 40);
tikhomirov@36: }
tikhomirov@169:
tikhomirov@37: public Nodeid cset() { // cs seems to be changeset
tikhomirov@36: return Nodeid.fromBinary(header, 60);
tikhomirov@36: }
tikhomirov@169:
tikhomirov@169: public DataAccess rawData() {
tikhomirov@169: return dataAccess;
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public List patches() throws IOException {
tikhomirov@169: if (patches == null) {
tikhomirov@169: dataAccess.reset();
tikhomirov@169: LinkedList p = new LinkedList();
tikhomirov@169: while (!dataAccess.isEmpty()) {
tikhomirov@169: RevlogStream.PatchRecord pr = RevlogStream.PatchRecord.read(dataAccess);
tikhomirov@169: p.add(pr);
tikhomirov@169: }
tikhomirov@169: patches = p;
tikhomirov@169: }
tikhomirov@169: return patches;
tikhomirov@169: }
tikhomirov@169:
tikhomirov@169: public byte[] apply(DataAccess baseContent) throws IOException {
tikhomirov@169: return RevlogStream.apply(baseContent, -1, patches());
tikhomirov@169: }
tikhomirov@36: }
tikhomirov@36: }