tikhomirov@0: /**
tikhomirov@0: * Copyright (c) 2010 Artem Tikhomirov
tikhomirov@0: */
tikhomirov@0: package com.tmate.hgkit.ll;
tikhomirov@0:
tikhomirov@0: import java.io.UnsupportedEncodingException;
tikhomirov@0: import java.util.ArrayList;
tikhomirov@0:
tikhomirov@0: /**
tikhomirov@0: * @see mercurial/changelog.py:read()
tikhomirov@0: *
tikhomirov@0: format used:
tikhomirov@0: nodeid\n : manifest node in ascii
tikhomirov@0: user\n : user, no \n or \r allowed
tikhomirov@0: time tz extra\n : date (time is int or float, timezone is int)
tikhomirov@0: : extra is metadatas, encoded and separated by '\0'
tikhomirov@0: : older versions ignore it
tikhomirov@0: files\n\n : files modified by the cset, no \n or \r allowed
tikhomirov@0: (.*) : comment (free text, ideally utf-8)
tikhomirov@0:
tikhomirov@0: changelog v0 doesn't use extra
tikhomirov@0: *
tikhomirov@0: * @author artem
tikhomirov@0: */
tikhomirov@0: public class Changeset {
tikhomirov@3: // TODO immutable
tikhomirov@0: private /*final*/ Nodeid nodeid;
tikhomirov@0: private String user;
tikhomirov@0: private String comment;
tikhomirov@0: private ArrayList files;
tikhomirov@0: private String timezone; // FIXME
tikhomirov@0:
tikhomirov@0: public void dump() {
tikhomirov@0: System.out.println("User:" + user);
tikhomirov@0: System.out.println("Comment:" + comment);
tikhomirov@0: System.out.println("Nodeid:" + nodeid);
tikhomirov@0: System.out.println("Date:" + timezone);
tikhomirov@0: System.out.println("Files: " + files.size());
tikhomirov@0: for (String s : files) {
tikhomirov@0: System.out.print('\t');
tikhomirov@0: System.out.println(s);
tikhomirov@0: }
tikhomirov@0: }
tikhomirov@0:
tikhomirov@3: public static Changeset parse(byte[] data, int offset, int length) {
tikhomirov@3: Changeset rv = new Changeset();
tikhomirov@0: final int bufferEndIndex = offset + length;
tikhomirov@0: final byte lineBreak = (byte) '\n';
tikhomirov@3: int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex);
tikhomirov@0: if (breakIndex1 == -1) {
tikhomirov@0: throw new IllegalArgumentException("Bad Changeset data");
tikhomirov@0: }
tikhomirov@3: rv.nodeid = Nodeid.fromAscii(data, 0, breakIndex1);
tikhomirov@3: int breakIndex2 = indexOf(data, lineBreak, breakIndex1+1, bufferEndIndex);
tikhomirov@0: if (breakIndex2 == -1) {
tikhomirov@0: throw new IllegalArgumentException("Bad Changeset data");
tikhomirov@0: }
tikhomirov@3: rv.user = new String(data, breakIndex1+1, breakIndex2 - breakIndex1 - 1);
tikhomirov@3: int breakIndex3 = indexOf(data, lineBreak, breakIndex2+1, bufferEndIndex);
tikhomirov@0: if (breakIndex3 == -1) {
tikhomirov@0: throw new IllegalArgumentException("Bad Changeset data");
tikhomirov@0: }
tikhomirov@3: rv.timezone = new String(data, breakIndex2+1, breakIndex3 - breakIndex2 - 1);
tikhomirov@0:
tikhomirov@0: //
tikhomirov@0: int lastStart = breakIndex3 + 1;
tikhomirov@3: int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
tikhomirov@3: rv.files = new ArrayList(5);
tikhomirov@0: while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) {
tikhomirov@3: rv.files.add(new String(data, lastStart, breakIndex4 - lastStart));
tikhomirov@0: lastStart = breakIndex4 + 1;
tikhomirov@3: if (data[breakIndex4 + 1] == lineBreak) {
tikhomirov@0: // found \n\n
tikhomirov@0: break;
tikhomirov@0: } else {
tikhomirov@3: breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
tikhomirov@0: }
tikhomirov@0: }
tikhomirov@0: if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) {
tikhomirov@0: throw new IllegalArgumentException("Bad Changeset data");
tikhomirov@0: }
tikhomirov@0: try {
tikhomirov@3: rv.comment = new String(data, breakIndex4+2, bufferEndIndex - breakIndex4 - 2, "UTF-8");
tikhomirov@0: } catch (UnsupportedEncodingException ex) {
tikhomirov@3: rv.comment = "";
tikhomirov@0: throw new IllegalStateException("Could hardly happen");
tikhomirov@0: }
tikhomirov@3: return rv;
tikhomirov@0: }
tikhomirov@0:
tikhomirov@0: private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) {
tikhomirov@0: for (int i = startOffset; i < endIndex; i++) {
tikhomirov@0: if (src[i] == what) {
tikhomirov@0: return i;
tikhomirov@0: }
tikhomirov@0: }
tikhomirov@0: return -1;
tikhomirov@0: }
tikhomirov@2:
tikhomirov@3: public interface Inspector {
tikhomirov@2: // first(), last(), single().
tikhomirov@2: //
tikhomirov@2: void next(Changeset cset);
tikhomirov@2: }
tikhomirov@0: }