annotate src/com/tmate/hgkit/ll/HgBundle.java @ 36:205f9b59b400

Strip parsing logic out from console frontend
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 13 Jan 2011 23:31:39 +0100
parents
children e45e75e22523
rev   line source
36
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 Artem Tikhomirov
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 */
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 package com.tmate.hgkit.ll;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 import java.io.File;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 import java.io.IOException;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 import java.util.LinkedList;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 import java.util.List;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 import com.tmate.hgkit.fs.DataAccess;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 import com.tmate.hgkit.fs.DataAccessProvider;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 /**
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * @see http://mercurial.selenic.com/wiki/BundleFormat
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 *
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 * @author artem
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18 */
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 public class HgBundle {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 private final File bundleFile;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 private final DataAccessProvider accessProvider;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 public HgBundle(DataAccessProvider dap, File bundle) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 accessProvider = dap;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 bundleFile = bundle;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 public void read() throws IOException {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 DataAccess da = accessProvider.create(bundleFile);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 try {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 LinkedList<String> names = new LinkedList<String>();
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 if (!da.isEmpty()) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 System.out.println("Changelog group");
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 List<GroupElement> changelogGroup = readGroup(da);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 for (GroupElement ge : changelogGroup) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 System.out.printf(" %s %s %s %s; patches:%d\n", ge.node(), ge.firstParent(), ge.secondParent(), ge.cs(), ge.patches.size());
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 System.out.println("Manifest group");
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 List<GroupElement> manifestGroup = readGroup(da);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 for (GroupElement ge : manifestGroup) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 System.out.printf(" %s %s %s %s; patches:%d\n", ge.node(), ge.firstParent(), ge.secondParent(), ge.cs(), ge.patches.size());
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 while (!da.isEmpty()) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 int fnameLen = da.readInt();
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 if (fnameLen <= 4) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 break; // null chunk, the last one.
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 byte[] fname = new byte[fnameLen - 4];
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 da.readBytes(fname, 0, fname.length);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 names.add(new String(fname));
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 List<GroupElement> fileGroup = readGroup(da);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 System.out.println(names.getLast());
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 for (GroupElement ge : fileGroup) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 System.out.printf(" %s %s %s %s; patches:%d\n", ge.node(), ge.firstParent(), ge.secondParent(), ge.cs(), ge.patches.size());
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 System.out.println(names.size());
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 for (String s : names) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 System.out.println(s);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 } finally {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 da.done();
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 private static List<GroupElement> readGroup(DataAccess da) throws IOException {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 int len = da.readInt();
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 LinkedList<GroupElement> rv = new LinkedList<HgBundle.GroupElement>();
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 while (len > 4 && !da.isEmpty()) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 byte[] nb = new byte[80];
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 da.readBytes(nb, 0, 80);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 int dataLength = len-84;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 LinkedList<RevlogStream.PatchRecord> patches = new LinkedList<RevlogStream.PatchRecord>();
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 while (dataLength > 0) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 RevlogStream.PatchRecord pr = RevlogStream.PatchRecord.read(da);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 patches.add(pr);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 dataLength -= pr.len + 12;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 rv.add(new GroupElement(nb, patches));
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 len = da.isEmpty() ? 0 : da.readInt();
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 return rv;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 static class GroupElement {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 private byte[] header; // byte[80] takes 120 bytes, 4 Nodeids - 192
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 private List<RevlogStream.PatchRecord> patches;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 GroupElement(byte[] fourNodeids, List<RevlogStream.PatchRecord> patchList) {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 assert fourNodeids != null && fourNodeids.length == 80;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 // patchList.size() > 0
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 header = fourNodeids;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 patches = patchList;
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 public Nodeid node() {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 return Nodeid.fromBinary(header, 0);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 public Nodeid firstParent() {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 return Nodeid.fromBinary(header, 20);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 public Nodeid secondParent() {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 return Nodeid.fromBinary(header, 40);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 public Nodeid cs() {
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 return Nodeid.fromBinary(header, 60);
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 }
205f9b59b400 Strip parsing logic out from console frontend
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 }