tikhomirov@21: /*
tikhomirov@589: * Copyright (c) 2010-2013 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@2: import java.util.ArrayList;
tikhomirov@3: import java.util.Arrays;
tikhomirov@146: import java.util.Calendar;
tikhomirov@129: import java.util.Date;
tikhomirov@129: import java.util.Formatter;
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@74: import org.tmatesoft.hg.core.Nodeid;
tikhomirov@667: import org.tmatesoft.hg.core.SessionContext;
tikhomirov@423: import org.tmatesoft.hg.internal.Callback;
tikhomirov@673: import org.tmatesoft.hg.internal.ChangesetParser;
tikhomirov@157: import org.tmatesoft.hg.internal.DataAccess;
tikhomirov@312: import org.tmatesoft.hg.internal.Lifecycle;
tikhomirov@520: import org.tmatesoft.hg.internal.LifecycleBridge;
tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream;
tikhomirov@520: import org.tmatesoft.hg.util.Adaptable;
tikhomirov@312: import org.tmatesoft.hg.util.CancelSupport;
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@426: public final class HgChangelog extends Revlog {
tikhomirov@0:
tikhomirov@153: /* package-local */HgChangelog(HgRepository hgRepo, RevlogStream content) {
tikhomirov@600: super(hgRepo, content, true);
tikhomirov@2: }
tikhomirov@2:
tikhomirov@628: /**
tikhomirov@628: * Iterate over whole changelog
tikhomirov@628: * @param inspector callback to process entries
tikhomirov@628: * @throws HgInvalidControlFileException if failed to access revlog index/data entry. Runtime exception
tikhomirov@628: * @throws HgRuntimeException subclass thereof to indicate other issues with the library. Runtime exception
tikhomirov@628: */
tikhomirov@628: public void all(final HgChangelog.Inspector inspector) throws HgRuntimeException {
tikhomirov@137: range(0, getLastRevision(), inspector);
tikhomirov@48: }
tikhomirov@48:
tikhomirov@628: /**
tikhomirov@628: * Iterate over changelog part
tikhomirov@628: * @param start first changelog entry to process
tikhomirov@628: * @param end last changelog entry to process
tikhomirov@628: * @param inspector callback to process entries
tikhomirov@628: * @throws HgInvalidRevisionException if any supplied revision doesn't identify revision from this revlog. Runtime exception
tikhomirov@628: * @throws HgInvalidControlFileException if failed to access revlog index/data entry. Runtime exception
tikhomirov@628: * @throws HgRuntimeException subclass thereof to indicate other issues with the library. Runtime exception
tikhomirov@628: */
tikhomirov@628: public void range(int start, int end, final HgChangelog.Inspector inspector) throws HgRuntimeException {
tikhomirov@196: if (inspector == null) {
tikhomirov@196: throw new IllegalArgumentException();
tikhomirov@196: }
tikhomirov@667: content.iterate(start, end, true, new RawCsetParser(getRepo(), inspector));
tikhomirov@2: }
tikhomirov@2:
tikhomirov@628: /**
tikhomirov@628: * @see #range(int, int, Inspector)
tikhomirov@628: * @return changeset entry objects, never null
tikhomirov@628: * @throws HgInvalidRevisionException if any supplied revision doesn't identify revision from this revlog. Runtime exception
tikhomirov@628: * @throws HgInvalidControlFileException if failed to access revlog index/data entry. Runtime exception
tikhomirov@628: * @throws HgRuntimeException subclass thereof to indicate other issues with the library. Runtime exception
tikhomirov@628: */
tikhomirov@628: public List range(int start, int end) throws HgRuntimeException {
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@628: * @throws HgInvalidRevisionException if any supplied revision doesn't identify revision from this revlog Runtime exception
tikhomirov@628: * @throws HgInvalidControlFileException if failed to access revlog index/data entry. Runtime exception
tikhomirov@628: * @throws HgRuntimeException subclass thereof to indicate other issues with the library. Runtime exception
tikhomirov@242: */
tikhomirov@628: public void range(final HgChangelog.Inspector inspector, final int... revisions) throws HgRuntimeException {
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@628: /*package-local*/ void rangeInternal(HgChangelog.Inspector inspector, int[] sortedRevisions) throws HgRuntimeException {
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@667: content.iterate(sortedRevisions, true, new RawCsetParser(getRepo(), inspector));
tikhomirov@3: }
tikhomirov@354:
tikhomirov@354: /**
tikhomirov@628: * Get changeset entry object
tikhomirov@628: * @throws HgInvalidRevisionException if supplied nodeid doesn't identify any revision from this revlog. Runtime exception
tikhomirov@628: * @throws HgInvalidControlFileException if failed to access revlog index/data entry. Runtime exception
tikhomirov@628: * @throws HgRuntimeException subclass thereof to indicate other issues with the library. Runtime exception
tikhomirov@354: */
tikhomirov@628: public RawChangeset changeset(Nodeid nid) throws HgRuntimeException {
tikhomirov@367: int x = getRevisionIndex(nid);
tikhomirov@236: return range(x, x).get(0);
tikhomirov@236: }
tikhomirov@129:
tikhomirov@423: @Callback
tikhomirov@129: public interface Inspector {
tikhomirov@423: /**
tikhomirov@423: * Access next changeset
tikhomirov@423: * TODO describe what revisionNumber is when Inspector is used with HgBundle (BAD_REVISION or bundle's local order?)
tikhomirov@423: *
tikhomirov@423: * @param revisionIndex index of revision being inspected, local to the inspected object
tikhomirov@423: * @param nodeid revision being inspected
tikhomirov@423: * @param cset changeset raw data
tikhomirov@423: */
tikhomirov@628: void next(int revisionIndex, Nodeid nodeid, RawChangeset cset) throws HgRuntimeException;
tikhomirov@129: }
tikhomirov@129:
tikhomirov@129: /**
tikhomirov@129: * Entry in the Changelog
tikhomirov@129: */
tikhomirov@667: public static final class RawChangeset implements Cloneable /* for those that would like to keep a copy */{
tikhomirov@667: // would be nice to get it immutable, but then we can't reuse instances
tikhomirov@673: /* final */Nodeid manifest;
tikhomirov@673: String user;
tikhomirov@673: String comment;
tikhomirov@673: String[] files; // shall not be modified (#clone() does shallow copy)
tikhomirov@673: Date time;
tikhomirov@673: 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@673: Map extras;
tikhomirov@129:
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@667: return Arrays.asList(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@667: sb.append("Files: ").append(files.length);
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@667: }
tikhomirov@667:
tikhomirov@673: /*package-local*/static final class RawCsetFactory implements ChangesetParser.CsetFactory {
tikhomirov@673: private RawChangeset cset;
tikhomirov@673:
tikhomirov@673: public RawCsetFactory(boolean shallReuseCsetInstance) {
tikhomirov@667: if (shallReuseCsetInstance) {
tikhomirov@673: cset = new RawChangeset();
tikhomirov@667: }
tikhomirov@667: }
tikhomirov@667:
tikhomirov@673: public RawChangeset create(Nodeid nodeidManifest, String user, Date time, int timezone, List files, String comment, Map extrasMap) {
tikhomirov@673: RawChangeset target;
tikhomirov@673: if (cset != null) {
tikhomirov@673: target = cset;
tikhomirov@673: } else {
tikhomirov@673: target = new RawChangeset();
tikhomirov@153: }
tikhomirov@673: target.manifest = nodeidManifest;
tikhomirov@673: target.user = user;
tikhomirov@673: target.time = time;
tikhomirov@673: target.timezone = timezone;
tikhomirov@673: target.files = files == null ? new String[0] : files.toArray(new String[files.size()]);
tikhomirov@673: target.comment = comment;
tikhomirov@673: target.extras = extrasMap;
tikhomirov@673: return target;
tikhomirov@363: }
tikhomirov@129: }
tikhomirov@673:
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@667: private static final class RawCsetParser implements RevlogStream.Inspector, Adaptable, Lifecycle {
tikhomirov@196:
tikhomirov@196: private final Inspector inspector;
tikhomirov@667: private final ChangesetParser csetBuilder;
tikhomirov@520: // non-null when inspector uses high-level lifecycle entities (progress and/or cancel supports)
tikhomirov@520: private final LifecycleBridge lifecycleStub;
tikhomirov@520: // non-null when inspector relies on low-level lifecycle and is responsible
tikhomirov@520: // to proceed any possible high-level entities himself.
tikhomirov@520: private final Lifecycle inspectorLifecycle;
tikhomirov@196:
tikhomirov@667: public RawCsetParser(SessionContext.Source sessionContext, HgChangelog.Inspector delegate) {
tikhomirov@196: assert delegate != null;
tikhomirov@196: inspector = delegate;
tikhomirov@673: csetBuilder = new ChangesetParser(sessionContext, new RawCsetFactory(true));
tikhomirov@520: inspectorLifecycle = Adaptable.Factory.getAdapter(delegate, Lifecycle.class, null);
tikhomirov@520: if (inspectorLifecycle == null) {
tikhomirov@520: ProgressSupport ph = Adaptable.Factory.getAdapter(delegate, ProgressSupport.class, null);
tikhomirov@520: CancelSupport cs = Adaptable.Factory.getAdapter(delegate, CancelSupport.class, null);
tikhomirov@520: if (cs != null || ph != null) {
tikhomirov@520: lifecycleStub = new LifecycleBridge(ph, cs);
tikhomirov@520: } else {
tikhomirov@520: lifecycleStub = null;
tikhomirov@520: }
tikhomirov@520: } else {
tikhomirov@520: lifecycleStub = null;
tikhomirov@520: }
tikhomirov@196: }
tikhomirov@196:
tikhomirov@628: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) throws HgRuntimeException {
tikhomirov@196: try {
tikhomirov@667: RawChangeset cset = csetBuilder.parse(da);
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@520: if (lifecycleStub != null) {
tikhomirov@520: lifecycleStub.nextStep();
tikhomirov@520: }
tikhomirov@427: } catch (HgInvalidDataFormatException ex) {
tikhomirov@427: throw ex.setRevisionIndex(revisionNumber);
tikhomirov@366: } catch (IOException ex) {
tikhomirov@423: // XXX need better exception, perhaps smth like HgChangelogException (extends HgInvalidControlFileException)
tikhomirov@423: throw new HgInvalidControlFileException("Failed reading changelog", ex, null).setRevisionIndex(revisionNumber);
tikhomirov@196: }
tikhomirov@520: }
tikhomirov@520:
tikhomirov@520: public T getAdapter(Class adapterClass) {
tikhomirov@520: if (adapterClass == Lifecycle.class) {
tikhomirov@667: return adapterClass.cast(this);
tikhomirov@312: }
tikhomirov@667: // XXX what if caller takes Progress/Cancel (which we update through lifecycleStub, too)
tikhomirov@520: return Adaptable.Factory.getAdapter(inspector, adapterClass, null);
tikhomirov@312: }
tikhomirov@312:
tikhomirov@667: public void start(int count, Callback callback, Object token) {
tikhomirov@667: if (inspectorLifecycle != null) {
tikhomirov@667: inspectorLifecycle.start(count, callback, token);
tikhomirov@667: } else if (lifecycleStub != null) {
tikhomirov@667: lifecycleStub.start(count, callback, token);
tikhomirov@667: }
tikhomirov@667: }
tikhomirov@667:
tikhomirov@667: public void finish(Object token) {
tikhomirov@667: if (inspectorLifecycle != null) {
tikhomirov@667: inspectorLifecycle.finish(token);
tikhomirov@667: } else if (lifecycleStub != null) {
tikhomirov@667: lifecycleStub.finish(token);
tikhomirov@667: }
tikhomirov@667: csetBuilder.dispose();
tikhomirov@667: }
tikhomirov@667:
tikhomirov@196: }
tikhomirov@0: }