tikhomirov@21: /* 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@157: import java.io.IOException; tikhomirov@129: import java.io.UnsupportedEncodingException; tikhomirov@2: import java.util.ArrayList; tikhomirov@3: import java.util.Arrays; tikhomirov@146: import java.util.Calendar; tikhomirov@317: import java.util.Collection; tikhomirov@129: import java.util.Collections; tikhomirov@129: import java.util.Date; tikhomirov@129: import java.util.Formatter; tikhomirov@129: import java.util.HashMap; tikhomirov@2: import java.util.List; tikhomirov@129: import java.util.Locale; tikhomirov@129: import java.util.Map; tikhomirov@146: import java.util.TimeZone; tikhomirov@2: tikhomirov@196: import org.tmatesoft.hg.core.HgBadStateException; tikhomirov@328: import org.tmatesoft.hg.core.HgLogCommand; tikhomirov@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@157: import org.tmatesoft.hg.internal.DataAccess; tikhomirov@312: import org.tmatesoft.hg.internal.IterateControlMediator; tikhomirov@312: import org.tmatesoft.hg.internal.Lifecycle; tikhomirov@196: import org.tmatesoft.hg.internal.Pool; tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream; tikhomirov@312: import org.tmatesoft.hg.util.CancelSupport; tikhomirov@317: import org.tmatesoft.hg.util.Pair; tikhomirov@312: import org.tmatesoft.hg.util.ProgressSupport; tikhomirov@74: tikhomirov@0: /** tikhomirov@0: * Representation of the Mercurial changelog file (list of ChangeSets) tikhomirov@153: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@0: */ tikhomirov@97: public class HgChangelog extends Revlog { tikhomirov@0: tikhomirov@153: /* package-local */HgChangelog(HgRepository hgRepo, RevlogStream content) { tikhomirov@21: super(hgRepo, content); tikhomirov@2: } tikhomirov@2: tikhomirov@129: public void all(final HgChangelog.Inspector inspector) { tikhomirov@137: range(0, getLastRevision(), inspector); tikhomirov@48: } tikhomirov@48: tikhomirov@129: public void range(int start, int end, final HgChangelog.Inspector inspector) { tikhomirov@196: if (inspector == null) { tikhomirov@196: throw new IllegalArgumentException(); tikhomirov@196: } tikhomirov@196: content.iterate(start, end, true, new RawCsetParser(inspector)); tikhomirov@2: } tikhomirov@2: tikhomirov@154: public List range(int start, int end) { tikhomirov@196: final RawCsetCollector c = new RawCsetCollector(end - start + 1); tikhomirov@196: range(start, end, c); tikhomirov@196: return c.result; tikhomirov@2: } tikhomirov@3: tikhomirov@242: /** tikhomirov@242: * Access individual revisions. Note, regardless of supplied revision order, inspector gets tikhomirov@242: * changesets strictly in the order they are in the changelog. tikhomirov@242: * @param inspector callback to get changesets tikhomirov@242: * @param revisions revisions to read, unrestricted ordering. tikhomirov@242: */ tikhomirov@129: public void range(final HgChangelog.Inspector inspector, final int... revisions) { tikhomirov@242: Arrays.sort(revisions); tikhomirov@242: rangeInternal(inspector, revisions); tikhomirov@242: } tikhomirov@242: tikhomirov@242: /** tikhomirov@242: * Friends-only version of {@link #range(Inspector, int...)}, when callers know array is sorted tikhomirov@242: */ tikhomirov@242: /*package-local*/ void rangeInternal(HgChangelog.Inspector inspector, int[] sortedRevisions) { tikhomirov@242: if (sortedRevisions == null || sortedRevisions.length == 0) { tikhomirov@3: return; tikhomirov@3: } tikhomirov@242: if (inspector == null) { tikhomirov@242: throw new IllegalArgumentException(); tikhomirov@242: } tikhomirov@242: content.iterate(sortedRevisions, true, new RawCsetParser(inspector)); tikhomirov@3: } tikhomirov@236: tikhomirov@236: public RawChangeset changeset(Nodeid nid) { tikhomirov@236: int x = getLocalRevision(nid); tikhomirov@236: return range(x, x).get(0); tikhomirov@236: } tikhomirov@129: tikhomirov@129: public interface Inspector { tikhomirov@129: // TODO describe whether cset is new instance each time tikhomirov@182: // describe what revisionNumber is when Inspector is used with HgBundle (BAD_REVISION or bundle's local order?) tikhomirov@154: void next(int revisionNumber, Nodeid nodeid, RawChangeset cset); tikhomirov@129: } tikhomirov@129: tikhomirov@129: /** tikhomirov@317: * Unlike regular {@link Inspector}, this one supplies changeset revision along with its parents and children according tikhomirov@317: * to parent information of the revlog this inspector visits. tikhomirov@317: * @see HgDataFile#history(TreeInspector) tikhomirov@328: * @deprecated use {@link HgChangesetTreeHandler} and HgLogCommand#execute(HgChangesetTreeHandler)} tikhomirov@317: */ tikhomirov@328: @Deprecated tikhomirov@317: public interface TreeInspector { tikhomirov@317: // the reason TreeInsector is in HgChangelog, not in Revlog, because despite the fact it can tikhomirov@317: // be applied to any revlog, it's not meant to provide revisions of any revlog it's beeing applied to, tikhomirov@317: // but changeset revisions always. tikhomirov@317: // TODO HgChangelog.walk(TreeInspector) tikhomirov@317: void next(Nodeid changesetRevision, Pair parentChangesets, Collection childChangesets); tikhomirov@317: } tikhomirov@317: tikhomirov@317: /** tikhomirov@129: * Entry in the Changelog tikhomirov@129: */ tikhomirov@154: public static class RawChangeset implements Cloneable /* for those that would like to keep a copy */{ tikhomirov@129: // TODO immutable tikhomirov@129: private/* final */Nodeid manifest; tikhomirov@153: private String user; tikhomirov@153: private String comment; tikhomirov@153: private List files; // unmodifiable collection (otherwise #files() and implicit #clone() shall be revised) tikhomirov@153: private Date time; tikhomirov@153: private int timezone; tikhomirov@161: // http://mercurial.selenic.com/wiki/PruningDeadBranches - Closing changesets can be identified by close=1 in the changeset's extra field. tikhomirov@153: private Map extras; tikhomirov@129: tikhomirov@153: /** tikhomirov@153: * @see mercurial/changelog.py:read() tikhomirov@153: * tikhomirov@153: *
tikhomirov@153: 		 *         format used:
tikhomirov@153: 		 *         nodeid\n        : manifest node in ascii
tikhomirov@153: 		 *         user\n          : user, no \n or \r allowed
tikhomirov@153: 		 *         time tz extra\n : date (time is int or float, timezone is int)
tikhomirov@153: 		 *                         : extra is metadatas, encoded and separated by '\0'
tikhomirov@153: 		 *                         : older versions ignore it
tikhomirov@153: 		 *         files\n\n       : files modified by the cset, no \n or \r allowed
tikhomirov@153: 		 *         (.*)            : comment (free text, ideally utf-8)
tikhomirov@153: 		 * 
tikhomirov@153: 		 *         changelog v0 doesn't use extra
tikhomirov@153: 		 * 
tikhomirov@153: */ tikhomirov@154: private RawChangeset() { tikhomirov@153: } tikhomirov@129: tikhomirov@153: public Nodeid manifest() { tikhomirov@153: return manifest; tikhomirov@153: } tikhomirov@129: tikhomirov@153: public String user() { tikhomirov@153: return user; tikhomirov@153: } tikhomirov@153: tikhomirov@153: public String comment() { tikhomirov@153: return comment; tikhomirov@153: } tikhomirov@153: tikhomirov@153: public List files() { tikhomirov@153: return files; tikhomirov@153: } tikhomirov@153: tikhomirov@153: public Date date() { tikhomirov@153: return time; tikhomirov@153: } tikhomirov@211: tikhomirov@211: /** tikhomirov@211: * @return time zone value, as is, positive for Western Hemisphere. tikhomirov@211: */ tikhomirov@211: public int timezone() { tikhomirov@211: return timezone; tikhomirov@211: } tikhomirov@153: tikhomirov@153: public String dateString() { tikhomirov@153: // XXX keep once formatted? Perhaps, there's faster way to set up calendar/time zone? tikhomirov@153: StringBuilder sb = new StringBuilder(30); tikhomirov@153: Formatter f = new Formatter(sb, Locale.US); tikhomirov@211: TimeZone tz = TimeZone.getTimeZone(TimeZone.getAvailableIDs(timezone * 1000)[0]); tikhomirov@153: // apparently timezone field records number of seconds time differs from UTC, tikhomirov@153: // i.e. value to substract from time to get UTC time. Calendar seems to add tikhomirov@153: // timezone offset to UTC, instead, hence sign change. tikhomirov@211: // tz.setRawOffset(timezone * -1000); tikhomirov@153: Calendar c = Calendar.getInstance(tz, Locale.US); tikhomirov@153: c.setTime(time); tikhomirov@153: f.format("%ta % extras() { tikhomirov@153: return extras; tikhomirov@153: } tikhomirov@153: tikhomirov@153: public String branch() { tikhomirov@153: return extras.get("branch"); tikhomirov@153: } tikhomirov@153: tikhomirov@153: @Override tikhomirov@153: public String toString() { tikhomirov@153: StringBuilder sb = new StringBuilder(); tikhomirov@153: sb.append("Changeset {"); tikhomirov@153: sb.append("User: ").append(user).append(", "); tikhomirov@153: sb.append("Comment: ").append(comment).append(", "); tikhomirov@153: sb.append("Manifest: ").append(manifest).append(", "); tikhomirov@153: sb.append("Date: ").append(time).append(", "); tikhomirov@153: sb.append("Files: ").append(files.size()); tikhomirov@153: for (String s : files) { tikhomirov@153: sb.append(", ").append(s); tikhomirov@153: } tikhomirov@153: if (extras != null) { tikhomirov@153: sb.append(", Extra: ").append(extras); tikhomirov@153: } tikhomirov@153: sb.append("}"); tikhomirov@153: return sb.toString(); tikhomirov@153: } tikhomirov@153: tikhomirov@153: @Override tikhomirov@154: public RawChangeset clone() { tikhomirov@153: try { tikhomirov@154: return (RawChangeset) super.clone(); tikhomirov@153: } catch (CloneNotSupportedException ex) { tikhomirov@153: throw new InternalError(ex.toString()); tikhomirov@153: } tikhomirov@153: } tikhomirov@153: tikhomirov@157: public static RawChangeset parse(DataAccess da) { tikhomirov@157: try { tikhomirov@157: byte[] data = da.byteArray(); tikhomirov@157: RawChangeset rv = new RawChangeset(); tikhomirov@196: rv.init(data, 0, data.length, null); tikhomirov@157: return rv; tikhomirov@157: } catch (IOException ex) { tikhomirov@196: throw new HgBadStateException(ex); // FIXME "Error reading changeset data" tikhomirov@157: } tikhomirov@153: } tikhomirov@153: tikhomirov@196: // @param usersPool - it's likely user names get repeated again and again throughout repository. can be null tikhomirov@196: /* package-local */void init(byte[] data, int offset, int length, Pool usersPool) { tikhomirov@153: final int bufferEndIndex = offset + length; tikhomirov@153: final byte lineBreak = (byte) '\n'; tikhomirov@153: int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex); tikhomirov@153: if (breakIndex1 == -1) { tikhomirov@153: throw new IllegalArgumentException("Bad Changeset data"); tikhomirov@153: } tikhomirov@153: Nodeid _nodeid = Nodeid.fromAscii(data, 0, breakIndex1); tikhomirov@153: int breakIndex2 = indexOf(data, lineBreak, breakIndex1 + 1, bufferEndIndex); tikhomirov@153: if (breakIndex2 == -1) { tikhomirov@153: throw new IllegalArgumentException("Bad Changeset data"); tikhomirov@153: } tikhomirov@153: String _user = new String(data, breakIndex1 + 1, breakIndex2 - breakIndex1 - 1); tikhomirov@196: if (usersPool != null) { tikhomirov@196: _user = usersPool.unify(_user); tikhomirov@196: } tikhomirov@153: int breakIndex3 = indexOf(data, lineBreak, breakIndex2 + 1, bufferEndIndex); tikhomirov@153: if (breakIndex3 == -1) { tikhomirov@153: throw new IllegalArgumentException("Bad Changeset data"); tikhomirov@153: } tikhomirov@153: String _timeString = new String(data, breakIndex2 + 1, breakIndex3 - breakIndex2 - 1); tikhomirov@153: int space1 = _timeString.indexOf(' '); tikhomirov@153: if (space1 == -1) { tikhomirov@153: throw new IllegalArgumentException("Bad Changeset data"); tikhomirov@153: } tikhomirov@153: int space2 = _timeString.indexOf(' ', space1 + 1); tikhomirov@153: if (space2 == -1) { tikhomirov@153: space2 = _timeString.length(); tikhomirov@153: } tikhomirov@153: long unixTime = Long.parseLong(_timeString.substring(0, space1)); // XXX Float, perhaps tikhomirov@153: int _timezone = Integer.parseInt(_timeString.substring(space1 + 1, space2)); tikhomirov@153: // 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@153: // on commit and timezone is recorded to adjust it to UTC. tikhomirov@153: Date _time = new Date(unixTime * 1000); tikhomirov@153: String _extras = space2 < _timeString.length() ? _timeString.substring(space2 + 1) : null; tikhomirov@153: Map _extrasMap; tikhomirov@252: final String extras_branch_key = "branch"; tikhomirov@153: if (_extras == null) { tikhomirov@252: _extrasMap = Collections.singletonMap(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME); tikhomirov@153: } else { tikhomirov@153: _extrasMap = new HashMap(); tikhomirov@153: for (String pair : _extras.split("\00")) { tikhomirov@153: int eq = pair.indexOf(':'); tikhomirov@153: // FIXME need to decode key/value, @see changelog.py:decodeextra tikhomirov@153: _extrasMap.put(pair.substring(0, eq), pair.substring(eq + 1)); tikhomirov@153: } tikhomirov@252: if (!_extrasMap.containsKey(extras_branch_key)) { tikhomirov@252: _extrasMap.put(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME); tikhomirov@153: } tikhomirov@153: _extrasMap = Collections.unmodifiableMap(_extrasMap); tikhomirov@153: } tikhomirov@153: tikhomirov@153: // tikhomirov@153: int lastStart = breakIndex3 + 1; tikhomirov@153: int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex); tikhomirov@153: ArrayList _files = null; tikhomirov@153: if (breakIndex4 > lastStart) { tikhomirov@153: // if breakIndex4 == lastStart, we already found \n\n and hence there are no files (e.g. merge revision) tikhomirov@153: _files = new ArrayList(5); tikhomirov@153: while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) { tikhomirov@153: _files.add(new String(data, lastStart, breakIndex4 - lastStart)); tikhomirov@153: lastStart = breakIndex4 + 1; tikhomirov@153: if (data[breakIndex4 + 1] == lineBreak) { tikhomirov@153: // found \n\n tikhomirov@153: break; tikhomirov@153: } else { tikhomirov@153: breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex); tikhomirov@129: } tikhomirov@153: } tikhomirov@153: if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) { tikhomirov@153: throw new IllegalArgumentException("Bad Changeset data"); tikhomirov@153: } tikhomirov@153: } else { tikhomirov@153: breakIndex4--; tikhomirov@153: } tikhomirov@153: String _comment; tikhomirov@153: try { tikhomirov@153: _comment = new String(data, breakIndex4 + 2, bufferEndIndex - breakIndex4 - 2, "UTF-8"); tikhomirov@153: // FIXME respect ui.fallbackencoding and try to decode if set tikhomirov@153: } catch (UnsupportedEncodingException ex) { tikhomirov@153: _comment = ""; tikhomirov@153: throw new IllegalStateException("Could hardly happen"); tikhomirov@153: } tikhomirov@153: // change this instance at once, don't leave it partially changes in case of error tikhomirov@153: this.manifest = _nodeid; tikhomirov@153: this.user = _user; tikhomirov@153: this.time = _time; tikhomirov@153: this.timezone = _timezone; tikhomirov@153: this.files = _files == null ? Collections. emptyList() : Collections.unmodifiableList(_files); tikhomirov@153: this.comment = _comment; tikhomirov@153: this.extras = _extrasMap; tikhomirov@153: } tikhomirov@129: tikhomirov@153: private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) { tikhomirov@153: for (int i = startOffset; i < endIndex; i++) { tikhomirov@153: if (src[i] == what) { tikhomirov@153: return i; tikhomirov@153: } tikhomirov@153: } tikhomirov@153: return -1; tikhomirov@153: } tikhomirov@129: } tikhomirov@129: tikhomirov@196: private static class RawCsetCollector implements Inspector { tikhomirov@196: final ArrayList result; tikhomirov@196: tikhomirov@196: public RawCsetCollector(int count) { tikhomirov@196: result = new ArrayList(count > 0 ? count : 5); tikhomirov@196: } tikhomirov@196: tikhomirov@196: public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) { tikhomirov@196: result.add(cset.clone()); tikhomirov@196: } tikhomirov@196: } tikhomirov@196: tikhomirov@312: private static class RawCsetParser implements RevlogStream.Inspector, Lifecycle { tikhomirov@196: tikhomirov@196: private final Inspector inspector; tikhomirov@196: private final Pool usersPool; tikhomirov@196: private final RawChangeset cset = new RawChangeset(); tikhomirov@312: private final ProgressSupport progressHelper; tikhomirov@312: private IterateControlMediator iterateControl; tikhomirov@196: tikhomirov@196: public RawCsetParser(HgChangelog.Inspector delegate) { tikhomirov@196: assert delegate != null; tikhomirov@196: inspector = delegate; tikhomirov@196: usersPool = new Pool(); tikhomirov@312: progressHelper = ProgressSupport.Factory.get(delegate); tikhomirov@196: } tikhomirov@196: tikhomirov@196: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { tikhomirov@196: try { tikhomirov@196: byte[] data = da.byteArray(); tikhomirov@196: cset.init(data, 0, data.length, usersPool); tikhomirov@196: // XXX there's no guarantee for Changeset.Callback that distinct instance comes each time, consider instance reuse tikhomirov@196: inspector.next(revisionNumber, Nodeid.fromBinary(nodeid, 0), cset); tikhomirov@312: progressHelper.worked(1); tikhomirov@196: } catch (Exception ex) { tikhomirov@196: throw new HgBadStateException(ex); // FIXME exception handling tikhomirov@196: } tikhomirov@312: if (iterateControl != null) { tikhomirov@312: iterateControl.checkCancelled(); tikhomirov@312: } tikhomirov@312: } tikhomirov@312: tikhomirov@312: public void start(int count, Callback callback, Object token) { tikhomirov@312: CancelSupport cs = CancelSupport.Factory.get(inspector, null); tikhomirov@312: iterateControl = cs == null ? null : new IterateControlMediator(cs, callback); tikhomirov@312: progressHelper.start(count); tikhomirov@312: } tikhomirov@312: tikhomirov@312: public void finish(Object token) { tikhomirov@312: progressHelper.done(); tikhomirov@196: } tikhomirov@196: } tikhomirov@0: }