Mercurial > jhg
comparison src/com/tmate/hgkit/ll/Changeset.java @ 0:dbd663faec1f
Basic changelog parsing
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 17 Dec 2010 19:05:59 +0100 |
parents | |
children | 08db726a0fb7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:dbd663faec1f |
---|---|
1 /** | |
2 * Copyright (c) 2010 Artem Tikhomirov | |
3 */ | |
4 package com.tmate.hgkit.ll; | |
5 | |
6 import java.io.UnsupportedEncodingException; | |
7 import java.util.ArrayList; | |
8 | |
9 /** | |
10 * @see mercurial/changelog.py:read() | |
11 * <pre> | |
12 format used: | |
13 nodeid\n : manifest node in ascii | |
14 user\n : user, no \n or \r allowed | |
15 time tz extra\n : date (time is int or float, timezone is int) | |
16 : extra is metadatas, encoded and separated by '\0' | |
17 : older versions ignore it | |
18 files\n\n : files modified by the cset, no \n or \r allowed | |
19 (.*) : comment (free text, ideally utf-8) | |
20 | |
21 changelog v0 doesn't use extra | |
22 * </pre> | |
23 * @author artem | |
24 */ | |
25 public class Changeset { | |
26 private /*final*/ Nodeid nodeid; | |
27 private String user; | |
28 private String comment; | |
29 private ArrayList<String> files; | |
30 private String timezone; // FIXME | |
31 public byte[] rawData; // FIXME | |
32 | |
33 public void dump() { | |
34 System.out.println("User:" + user); | |
35 System.out.println("Comment:" + comment); | |
36 System.out.println("Nodeid:" + nodeid); | |
37 System.out.println("Date:" + timezone); | |
38 System.out.println("Files: " + files.size()); | |
39 for (String s : files) { | |
40 System.out.print('\t'); | |
41 System.out.println(s); | |
42 } | |
43 } | |
44 | |
45 public void read(byte[] buf, int offset, int length) { | |
46 rawData = new byte[length]; | |
47 System.arraycopy(buf, offset, rawData, 0, length); | |
48 final int bufferEndIndex = offset + length; | |
49 final byte lineBreak = (byte) '\n'; | |
50 int breakIndex1 = indexOf(buf, lineBreak, offset, bufferEndIndex); | |
51 if (breakIndex1 == -1) { | |
52 throw new IllegalArgumentException("Bad Changeset data"); | |
53 } | |
54 nodeid = Nodeid.fromAscii(buf, 0, breakIndex1); | |
55 int breakIndex2 = indexOf(buf, lineBreak, breakIndex1+1, bufferEndIndex); | |
56 if (breakIndex2 == -1) { | |
57 throw new IllegalArgumentException("Bad Changeset data"); | |
58 } | |
59 user = new String(buf, breakIndex1+1, breakIndex2 - breakIndex1 - 1); | |
60 int breakIndex3 = indexOf(buf, lineBreak, breakIndex2+1, bufferEndIndex); | |
61 if (breakIndex3 == -1) { | |
62 throw new IllegalArgumentException("Bad Changeset data"); | |
63 } | |
64 timezone = new String(buf, breakIndex2+1, breakIndex3 - breakIndex2 - 1); | |
65 | |
66 // | |
67 int lastStart = breakIndex3 + 1; | |
68 int breakIndex4 = indexOf(buf, lineBreak, lastStart, bufferEndIndex); | |
69 files = new ArrayList<String>(5); | |
70 while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) { | |
71 files.add(new String(buf, lastStart, breakIndex4 - lastStart)); | |
72 lastStart = breakIndex4 + 1; | |
73 if (buf[breakIndex4 + 1] == lineBreak) { | |
74 // found \n\n | |
75 break; | |
76 } else { | |
77 breakIndex4 = indexOf(buf, lineBreak, lastStart, bufferEndIndex); | |
78 } | |
79 } | |
80 if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) { | |
81 throw new IllegalArgumentException("Bad Changeset data"); | |
82 } | |
83 try { | |
84 comment = new String(buf, breakIndex4+2, bufferEndIndex - breakIndex4 - 2, "UTF-8"); | |
85 } catch (UnsupportedEncodingException ex) { | |
86 comment = ""; | |
87 throw new IllegalStateException("Could hardly happen"); | |
88 } | |
89 } | |
90 | |
91 private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) { | |
92 for (int i = startOffset; i < endIndex; i++) { | |
93 if (src[i] == what) { | |
94 return i; | |
95 } | |
96 } | |
97 return -1; | |
98 } | |
99 } |