tikhomirov@38: /*
tikhomirov@74: * Copyright (c) 2010-2011 TMate Software Ltd
tikhomirov@74: *
tikhomirov@74: * This program is free software; you can redistribute it and/or modify
tikhomirov@74: * it under the terms of the GNU General Public License as published by
tikhomirov@74: * the Free Software Foundation; version 2 of the License.
tikhomirov@74: *
tikhomirov@74: * This program is distributed in the hope that it will be useful,
tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@74: * GNU General Public License for more details.
tikhomirov@74: *
tikhomirov@74: * For information on how to redistribute this software under
tikhomirov@74: * the terms of a license other than GNU General Public License
tikhomirov@102: * contact TMate Software at support@hg4j.com
tikhomirov@0: */
tikhomirov@74: package org.tmatesoft.hg.repo;
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@49: import java.util.HashMap;
tikhomirov@47: import java.util.List;
tikhomirov@40: import java.util.Locale;
tikhomirov@47: import java.util.Map;
tikhomirov@0:
tikhomirov@74: import org.tmatesoft.hg.core.Nodeid;
tikhomirov@74:
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@74: *
tikhomirov@74: * @author Artem Tikhomirov
tikhomirov@74: * @author TMate Software Ltd.
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@49: private Map extras;
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@49: return extras;
tikhomirov@49: }
tikhomirov@49:
tikhomirov@49: public String branch() {
tikhomirov@49: return extras.get("branch");
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@64:
tikhomirov@64: @Override
tikhomirov@64: public Changeset clone() {
tikhomirov@64: try {
tikhomirov@64: return (Changeset) super.clone();
tikhomirov@64: } catch (CloneNotSupportedException ex) {
tikhomirov@64: throw new InternalError(ex.toString());
tikhomirov@64: }
tikhomirov@64: }
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@49: Map _extrasMap;
tikhomirov@49: if (_extras == null) {
tikhomirov@49: _extrasMap = Collections.singletonMap("branch", "default");
tikhomirov@49: } else {
tikhomirov@49: _extrasMap = new HashMap();
tikhomirov@49: for (String pair : _extras.split("\00")) {
tikhomirov@54: int eq = pair.indexOf(':');
tikhomirov@49: // FIXME need to decode key/value, @see changelog.py:decodeextra
tikhomirov@49: _extrasMap.put(pair.substring(0, eq), pair.substring(eq+1));
tikhomirov@49: }
tikhomirov@49: if (!_extrasMap.containsKey("branch")) {
tikhomirov@49: _extrasMap.put("branch", "default");
tikhomirov@49: }
tikhomirov@49: _extrasMap = Collections.unmodifiableMap(_extrasMap);
tikhomirov@49: }
tikhomirov@0:
tikhomirov@0: //
tikhomirov@0: int lastStart = breakIndex3 + 1;
tikhomirov@3: int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
tikhomirov@87: ArrayList _files = null;
tikhomirov@87: if (breakIndex4 > lastStart) {
tikhomirov@87: // if breakIndex4 == lastStart, we already found \n\n and hence there are no files (e.g. merge revision)
tikhomirov@87: _files = new ArrayList(5);
tikhomirov@87: while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) {
tikhomirov@87: _files.add(new String(data, lastStart, breakIndex4 - lastStart));
tikhomirov@87: lastStart = breakIndex4 + 1;
tikhomirov@87: if (data[breakIndex4 + 1] == lineBreak) {
tikhomirov@87: // found \n\n
tikhomirov@87: break;
tikhomirov@87: } else {
tikhomirov@87: breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
tikhomirov@87: }
tikhomirov@0: }
tikhomirov@87: if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) {
tikhomirov@87: throw new IllegalArgumentException("Bad Changeset data");
tikhomirov@87: }
tikhomirov@87: } else {
tikhomirov@87: breakIndex4--;
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@87: this.files = _files == null ? Collections.emptyList() : Collections.unmodifiableList(_files);
tikhomirov@5: this.comment = _comment;
tikhomirov@49: this.extras = _extrasMap;
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: }