comparison src/com/tmate/hgkit/ll/Changeset.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 fc265ddeab26
comparison
equal deleted inserted replaced
2:08db726a0fb7 3:24bb4f365164
21 changelog v0 doesn't use extra 21 changelog v0 doesn't use extra
22 * </pre> 22 * </pre>
23 * @author artem 23 * @author artem
24 */ 24 */
25 public class Changeset { 25 public class Changeset {
26 // TODO immutable
26 private /*final*/ Nodeid nodeid; 27 private /*final*/ Nodeid nodeid;
27 private String user; 28 private String user;
28 private String comment; 29 private String comment;
29 private ArrayList<String> files; 30 private ArrayList<String> files;
30 private String timezone; // FIXME 31 private String timezone; // FIXME
31 public byte[] rawData; // FIXME
32 32
33 public void dump() { 33 public void dump() {
34 System.out.println("User:" + user); 34 System.out.println("User:" + user);
35 System.out.println("Comment:" + comment); 35 System.out.println("Comment:" + comment);
36 System.out.println("Nodeid:" + nodeid); 36 System.out.println("Nodeid:" + nodeid);
40 System.out.print('\t'); 40 System.out.print('\t');
41 System.out.println(s); 41 System.out.println(s);
42 } 42 }
43 } 43 }
44 44
45 public void read(byte[] buf, int offset, int length) { 45 public static Changeset parse(byte[] data, int offset, int length) {
46 rawData = new byte[length]; 46 Changeset rv = new Changeset();
47 System.arraycopy(buf, offset, rawData, 0, length);
48 final int bufferEndIndex = offset + length; 47 final int bufferEndIndex = offset + length;
49 final byte lineBreak = (byte) '\n'; 48 final byte lineBreak = (byte) '\n';
50 int breakIndex1 = indexOf(buf, lineBreak, offset, bufferEndIndex); 49 int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex);
51 if (breakIndex1 == -1) { 50 if (breakIndex1 == -1) {
52 throw new IllegalArgumentException("Bad Changeset data"); 51 throw new IllegalArgumentException("Bad Changeset data");
53 } 52 }
54 nodeid = Nodeid.fromAscii(buf, 0, breakIndex1); 53 rv.nodeid = Nodeid.fromAscii(data, 0, breakIndex1);
55 int breakIndex2 = indexOf(buf, lineBreak, breakIndex1+1, bufferEndIndex); 54 int breakIndex2 = indexOf(data, lineBreak, breakIndex1+1, bufferEndIndex);
56 if (breakIndex2 == -1) { 55 if (breakIndex2 == -1) {
57 throw new IllegalArgumentException("Bad Changeset data"); 56 throw new IllegalArgumentException("Bad Changeset data");
58 } 57 }
59 user = new String(buf, breakIndex1+1, breakIndex2 - breakIndex1 - 1); 58 rv.user = new String(data, breakIndex1+1, breakIndex2 - breakIndex1 - 1);
60 int breakIndex3 = indexOf(buf, lineBreak, breakIndex2+1, bufferEndIndex); 59 int breakIndex3 = indexOf(data, lineBreak, breakIndex2+1, bufferEndIndex);
61 if (breakIndex3 == -1) { 60 if (breakIndex3 == -1) {
62 throw new IllegalArgumentException("Bad Changeset data"); 61 throw new IllegalArgumentException("Bad Changeset data");
63 } 62 }
64 timezone = new String(buf, breakIndex2+1, breakIndex3 - breakIndex2 - 1); 63 rv.timezone = new String(data, breakIndex2+1, breakIndex3 - breakIndex2 - 1);
65 64
66 // 65 //
67 int lastStart = breakIndex3 + 1; 66 int lastStart = breakIndex3 + 1;
68 int breakIndex4 = indexOf(buf, lineBreak, lastStart, bufferEndIndex); 67 int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
69 files = new ArrayList<String>(5); 68 rv.files = new ArrayList<String>(5);
70 while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) { 69 while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) {
71 files.add(new String(buf, lastStart, breakIndex4 - lastStart)); 70 rv.files.add(new String(data, lastStart, breakIndex4 - lastStart));
72 lastStart = breakIndex4 + 1; 71 lastStart = breakIndex4 + 1;
73 if (buf[breakIndex4 + 1] == lineBreak) { 72 if (data[breakIndex4 + 1] == lineBreak) {
74 // found \n\n 73 // found \n\n
75 break; 74 break;
76 } else { 75 } else {
77 breakIndex4 = indexOf(buf, lineBreak, lastStart, bufferEndIndex); 76 breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
78 } 77 }
79 } 78 }
80 if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) { 79 if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) {
81 throw new IllegalArgumentException("Bad Changeset data"); 80 throw new IllegalArgumentException("Bad Changeset data");
82 } 81 }
83 try { 82 try {
84 comment = new String(buf, breakIndex4+2, bufferEndIndex - breakIndex4 - 2, "UTF-8"); 83 rv.comment = new String(data, breakIndex4+2, bufferEndIndex - breakIndex4 - 2, "UTF-8");
85 } catch (UnsupportedEncodingException ex) { 84 } catch (UnsupportedEncodingException ex) {
86 comment = ""; 85 rv.comment = "";
87 throw new IllegalStateException("Could hardly happen"); 86 throw new IllegalStateException("Could hardly happen");
88 } 87 }
88 return rv;
89 } 89 }
90 90
91 private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) { 91 private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) {
92 for (int i = startOffset; i < endIndex; i++) { 92 for (int i = startOffset; i < endIndex; i++) {
93 if (src[i] == what) { 93 if (src[i] == what) {
95 } 95 }
96 } 96 }
97 return -1; 97 return -1;
98 } 98 }
99 99
100 public interface Callback { 100 public interface Inspector {
101 // first(), last(), single(). 101 // first(), last(), single().
102 // <T> 102 // <T>
103 void next(Changeset cset); 103 void next(Changeset cset);
104 } 104 }
105 } 105 }