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