Mercurial > jhg
comparison src/com/tmate/hgkit/console/Main.java @ 3:24bb4f365164
Rudimentary log functionality with basic infrastructure is in place
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 20 Dec 2010 02:50:36 +0100 |
parents | 08db726a0fb7 |
children | aa1912c70b36 |
comparison
equal
deleted
inserted
replaced
2:08db726a0fb7 | 3:24bb4f365164 |
---|---|
4 import java.io.DataInput; | 4 import java.io.DataInput; |
5 import java.io.DataInputStream; | 5 import java.io.DataInputStream; |
6 import java.io.File; | 6 import java.io.File; |
7 import java.io.FileInputStream; | 7 import java.io.FileInputStream; |
8 import java.math.BigInteger; | 8 import java.math.BigInteger; |
9 import java.util.ArrayList; | |
10 import java.util.Collections; | |
11 import java.util.Iterator; | |
12 import java.util.LinkedList; | 9 import java.util.LinkedList; |
13 import java.util.List; | |
14 import java.util.zip.Deflater; | |
15 import java.util.zip.Inflater; | 10 import java.util.zip.Inflater; |
16 | 11 |
17 import com.tmate.hgkit.ll.Changeset; | 12 import com.tmate.hgkit.ll.Changeset; |
18 | 13 |
19 /** | 14 /** |
21 * @author artem | 16 * @author artem |
22 */ | 17 */ |
23 public class Main { | 18 public class Main { |
24 | 19 |
25 public static void main(String[] args) throws Exception { | 20 public static void main(String[] args) throws Exception { |
21 //String filename = "store/00changelog.i"; | |
22 String filename = "store/data/hello.c.i"; | |
26 LinkedList<Changeset> changelog = new LinkedList<Changeset>(); | 23 LinkedList<Changeset> changelog = new LinkedList<Changeset>(); |
27 // | 24 // |
28 DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("/temp/hg/hello/" + ".hg/store/00changelog.i")))); | 25 DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("/temp/hg/hello/.hg/" + filename)))); |
29 DataInput di = dis; | 26 DataInput di = dis; |
30 dis.mark(10); | 27 dis.mark(10); |
31 int versionField = di.readInt(); | 28 int versionField = di.readInt(); |
32 dis.reset(); | 29 dis.reset(); |
33 final int INLINEDATA = 1 << 16; | 30 final int INLINEDATA = 1 << 16; |
49 byte[] buf = new byte[32]; | 46 byte[] buf = new byte[32]; |
50 di.readFully(buf, 12, 20); | 47 di.readFully(buf, 12, 20); |
51 dis.skip(12); | 48 dis.skip(12); |
52 System.out.printf("%14d %6X %10d %10d %10d %10d %8d %8d %040x\n", offset, flags, compressedLen, actualLen, baseRevision, linkRevision, parent1Revision, parent2Revision, new BigInteger(buf)); | 49 System.out.printf("%14d %6X %10d %10d %10d %10d %8d %8d %040x\n", offset, flags, compressedLen, actualLen, baseRevision, linkRevision, parent1Revision, parent2Revision, new BigInteger(buf)); |
53 if (inlineData) { | 50 if (inlineData) { |
51 String resultString; | |
54 byte[] data = new byte[compressedLen]; | 52 byte[] data = new byte[compressedLen]; |
55 di.readFully(data); | 53 di.readFully(data); |
56 if (data[0] == 0x78 /* 'x' */) { | 54 if (data[0] == 0x78 /* 'x' */) { |
57 Inflater zlib = new Inflater(); | 55 Inflater zlib = new Inflater(); |
58 zlib.setInput(data, 0, compressedLen); | 56 zlib.setInput(data, 0, compressedLen); |
59 byte[] result = new byte[actualLen*2]; | 57 byte[] result = new byte[actualLen*2]; |
60 int resultLen = zlib.inflate(result); | 58 int resultLen = zlib.inflate(result); |
61 zlib.end(); | 59 zlib.end(); |
62 if (resultLen != actualLen) { | |
63 System.err.printf("Expected:%d, decomressed to:%d bytes\n", actualLen, resultLen); | |
64 } | |
65 String resultString; | |
66 if (baseRevision != entryCount) { | |
67 // this is a patch | |
68 byte[] baseRevContent = changelog.get(baseRevision).rawData; | |
69 LinkedList<PatchRecord> bins = new LinkedList<PatchRecord>(); | |
70 int p1, p2, len, patchElementIndex = 0; | |
71 do { | |
72 final int x = patchElementIndex; | |
73 p1 = (result[x] << 24) | (result[x+1] << 16) | (result[x+2] << 8) | result[x+3]; | |
74 p2 = (result[x+4] << 24) | (result[x+5] << 16) | (result[x+6] << 8) | result[x+7]; | |
75 len = (result[x+8] << 24) | (result[x+9] << 16) | (result[x+10] << 8) | result[x+11]; | |
76 System.out.printf("%4d %4d %4d\n", p1, p2, len); | |
77 patchElementIndex += 12 + len; | |
78 bins.add(new PatchRecord(p1, p2, len, result, x+12)); | |
79 } while (patchElementIndex < resultLen); | |
80 // | |
81 result = apply(baseRevContent, bins); | |
82 resultLen = result.length; | |
83 } | |
84 resultString = new String(result, 0, resultLen, "UTF-8"); | 60 resultString = new String(result, 0, resultLen, "UTF-8"); |
85 System.out.println(resultString); | 61 } else if (data[0] == 0x75 /* 'u' */) { |
86 entryCount++; | 62 resultString = new String(data, 1, data.length - 1, "UTF-8"); |
87 Changeset changeset = new Changeset(); | 63 } else { |
88 changeset.read(result, 0, resultLen); | 64 resultString = new String(data); |
89 changelog.add(changeset); | 65 } |
90 } // TODO else if uncompressed | 66 System.out.println(resultString); |
91 } | 67 } |
92 } | 68 } |
93 dis.close(); | 69 dis.close(); |
94 // | 70 // |
95 System.out.println("\n\n"); | 71 System.out.println("\n\n"); |
98 System.out.println(">"); | 74 System.out.println(">"); |
99 cset.dump(); | 75 cset.dump(); |
100 System.out.println("<"); | 76 System.out.println("<"); |
101 } | 77 } |
102 } | 78 } |
103 | |
104 | |
105 // mpatch.c : apply() | |
106 private static byte[] apply(byte[] baseRevisionContent, List<PatchRecord> patch) { | |
107 byte[] tempBuf = new byte[512]; // XXX | |
108 int last = 0, destIndex = 0; | |
109 for (PatchRecord pr : patch) { | |
110 System.arraycopy(baseRevisionContent, last, tempBuf, destIndex, pr.start-last); | |
111 destIndex += pr.start - last; | |
112 System.arraycopy(pr.data, 0, tempBuf, destIndex, pr.data.length); | |
113 destIndex += pr.data.length; | |
114 last = pr.end; | |
115 } | |
116 System.arraycopy(baseRevisionContent, last, tempBuf, destIndex, baseRevisionContent.length - last); | |
117 destIndex += baseRevisionContent.length - last; // total length | |
118 byte[] rv = new byte[destIndex]; | |
119 System.arraycopy(tempBuf, 0, rv, 0, destIndex); | |
120 return rv; | |
121 } | |
122 | |
123 static class PatchRecord { // copy of struct frag from mpatch.c | |
124 int start, end, len; | |
125 byte[] data; | |
126 | |
127 public PatchRecord(int p1, int p2, int len, byte[] src, int srcOffset) { | |
128 start = p1; | |
129 end = p2; | |
130 this.len = len; | |
131 data = new byte[len]; | |
132 System.arraycopy(src, srcOffset, data, 0, len); | |
133 } | |
134 } | |
135 } | 79 } |