tikhomirov@38: /* tikhomirov@38: * Copyright (c) 2010, 2011 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@47: import java.util.Collections; tikhomirov@40: import java.util.Date; tikhomirov@47: import java.util.Formatter; tikhomirov@47: import java.util.List; tikhomirov@40: import java.util.Locale; tikhomirov@47: import java.util.Map; 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@47: public class Changeset implements Cloneable /*for those that would like to keep a copy*/ { tikhomirov@3: // TODO immutable tikhomirov@38: private /*final*/ Nodeid manifest; tikhomirov@0: private String user; tikhomirov@0: private String comment; tikhomirov@47: private List files; // unmodifiable collection (otherwise #files() and implicit #clone() shall be revised) tikhomirov@40: private Date time; tikhomirov@47: private int timezone; // not sure it's of any use tikhomirov@40: private String extras; // TODO branch, etc. tikhomirov@0: tikhomirov@47: private Changeset() { tikhomirov@47: } tikhomirov@47: tikhomirov@47: public Nodeid manifest() { tikhomirov@47: return manifest; tikhomirov@47: } tikhomirov@47: tikhomirov@47: public String user() { tikhomirov@47: return user; tikhomirov@47: } tikhomirov@47: tikhomirov@47: public String comment() { tikhomirov@47: return comment; tikhomirov@47: } tikhomirov@47: tikhomirov@47: public List files() { tikhomirov@47: return files; tikhomirov@47: } tikhomirov@47: tikhomirov@47: public Date date() { tikhomirov@47: return time; tikhomirov@47: } tikhomirov@47: tikhomirov@47: public String dateString() { tikhomirov@47: StringBuilder sb = new StringBuilder(30); tikhomirov@47: Formatter f = new Formatter(sb, Locale.US); tikhomirov@47: f.format("%ta % extras() { tikhomirov@47: return null; // TODO tikhomirov@47: } tikhomirov@47: tikhomirov@47: @Override tikhomirov@47: public String toString() { tikhomirov@47: StringBuilder sb = new StringBuilder(); tikhomirov@47: sb.append("Changeset {"); tikhomirov@47: sb.append("User: ").append(user).append(", "); tikhomirov@47: sb.append("Comment: ").append(comment).append(", "); tikhomirov@47: sb.append("Manifest: ").append(manifest).append(", "); tikhomirov@47: sb.append("Date: ").append(time).append(", "); tikhomirov@47: sb.append("Files: ").append(files.size()); tikhomirov@47: for (String s : files) { tikhomirov@47: sb.append(", ").append(s); tikhomirov@47: } tikhomirov@40: if (extras != null) { tikhomirov@47: sb.append(", Extra: ").append(extras); tikhomirov@40: } tikhomirov@47: sb.append("}"); tikhomirov@47: return sb.toString(); tikhomirov@0: } tikhomirov@0: tikhomirov@3: public static Changeset parse(byte[] data, int offset, int length) { tikhomirov@3: Changeset rv = new Changeset(); tikhomirov@5: rv.init(data, offset, length); tikhomirov@5: return rv; tikhomirov@5: } tikhomirov@5: tikhomirov@5: /*package-local*/ void init(byte[] data, int offset, int length) { 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@5: Nodeid _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@5: String _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@40: String _timeString = new String(data, breakIndex2+1, breakIndex3 - breakIndex2 - 1); tikhomirov@40: int space1 = _timeString.indexOf(' '); tikhomirov@40: if (space1 == -1) { tikhomirov@40: throw new IllegalArgumentException("Bad Changeset data"); tikhomirov@40: } tikhomirov@40: int space2 = _timeString.indexOf(' ', space1+1); tikhomirov@40: if (space2 == -1) { tikhomirov@40: space2 = _timeString.length(); tikhomirov@40: } tikhomirov@40: long unixTime = Long.parseLong(_timeString.substring(0, space1)); // XXX Float, perhaps tikhomirov@47: int _timezone = Integer.parseInt(_timeString.substring(space1+1, space2)); tikhomirov@40: // XXX not sure need to add timezone here - I can't figure out whether Hg keeps GMT time, and records timezone just for info, or unixTime is taken local tikhomirov@40: // on commit and timezone is recorded to adjust it to UTC. tikhomirov@47: Date _time = new Date(unixTime * 1000); tikhomirov@40: String _extras = space2 < _timeString.length() ? _timeString.substring(space2+1) : null; tikhomirov@0: tikhomirov@0: // tikhomirov@0: int lastStart = breakIndex3 + 1; tikhomirov@3: int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex); tikhomirov@5: ArrayList _files = new ArrayList(5); tikhomirov@0: while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) { tikhomirov@5: _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@5: String _comment; tikhomirov@0: try { tikhomirov@5: _comment = new String(data, breakIndex4+2, bufferEndIndex - breakIndex4 - 2, "UTF-8"); tikhomirov@0: } catch (UnsupportedEncodingException ex) { tikhomirov@5: _comment = ""; tikhomirov@0: throw new IllegalStateException("Could hardly happen"); tikhomirov@0: } tikhomirov@5: // change this instance at once, don't leave it partially changes in case of error tikhomirov@38: this.manifest = _nodeid; tikhomirov@5: this.user = _user; tikhomirov@40: this.time = _time; tikhomirov@47: this.timezone = _timezone; tikhomirov@47: this.files = Collections.unmodifiableList(_files); tikhomirov@5: this.comment = _comment; tikhomirov@40: this.extras = _extras; 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@47: // TODO describe whether cset is new instance each time tikhomirov@47: void next(int revisionNumber, Nodeid nodeid, Changeset cset); tikhomirov@2: } tikhomirov@0: }