tikhomirov@13: /* tikhomirov@390: * Copyright (c) 2010-2012 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@2: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@74: tikhomirov@390: import static org.tmatesoft.hg.core.Nodeid.NULL; tikhomirov@423: import static org.tmatesoft.hg.repo.HgRepository.*; tikhomirov@218: tikhomirov@232: import java.io.ByteArrayOutputStream; tikhomirov@157: import java.io.IOException; tikhomirov@218: import java.util.ArrayList; tikhomirov@218: import java.util.Arrays; tikhomirov@326: import java.util.HashMap; tikhomirov@326: import java.util.Map; tikhomirov@157: tikhomirov@417: import org.tmatesoft.hg.core.HgChangesetFileSneaker; tikhomirov@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@423: import org.tmatesoft.hg.internal.Callback; tikhomirov@157: import org.tmatesoft.hg.internal.DataAccess; tikhomirov@266: import org.tmatesoft.hg.internal.DigestHelper; tikhomirov@320: import org.tmatesoft.hg.internal.EncodingHelper; tikhomirov@232: import org.tmatesoft.hg.internal.Experimental; tikhomirov@415: import org.tmatesoft.hg.internal.IntMap; tikhomirov@312: import org.tmatesoft.hg.internal.IterateControlMediator; tikhomirov@218: import org.tmatesoft.hg.internal.Lifecycle; tikhomirov@304: import org.tmatesoft.hg.internal.Pool2; tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream; tikhomirov@312: import org.tmatesoft.hg.util.CancelSupport; tikhomirov@232: import org.tmatesoft.hg.util.Path; tikhomirov@312: import org.tmatesoft.hg.util.ProgressSupport; tikhomirov@74: tikhomirov@2: tikhomirov@2: /** tikhomirov@2: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@2: */ tikhomirov@2: public class HgManifest extends Revlog { tikhomirov@218: private RevisionMapper revisionMap; tikhomirov@412: private EncodingHelper encodingHelper; tikhomirov@285: tikhomirov@415: /** tikhomirov@415: * File flags recorded in manifest tikhomirov@415: */ tikhomirov@285: public enum Flags { tikhomirov@415: /** tikhomirov@415: * Executable bit set tikhomirov@415: */ tikhomirov@415: Exec, tikhomirov@415: /** tikhomirov@415: * Symbolic link tikhomirov@415: */ tikhomirov@415: Link, tikhomirov@415: /** tikhomirov@415: * Regular file tikhomirov@415: */ tikhomirov@415: RegularFile; tikhomirov@285: tikhomirov@285: static Flags parse(String flags) { tikhomirov@285: if ("x".equalsIgnoreCase(flags)) { tikhomirov@285: return Exec; tikhomirov@285: } tikhomirov@285: if ("l".equalsIgnoreCase(flags)) { tikhomirov@285: return Link; tikhomirov@285: } tikhomirov@285: if (flags == null) { tikhomirov@415: return RegularFile; tikhomirov@285: } tikhomirov@285: throw new IllegalStateException(flags); tikhomirov@285: } tikhomirov@285: tikhomirov@285: static Flags parse(byte[] data, int start, int length) { tikhomirov@285: if (length == 0) { tikhomirov@415: return RegularFile; tikhomirov@285: } tikhomirov@285: if (length == 1) { tikhomirov@285: if (data[start] == 'x') { tikhomirov@285: return Exec; tikhomirov@285: } tikhomirov@285: if (data[start] == 'l') { tikhomirov@285: return Link; tikhomirov@285: } tikhomirov@285: // FALL THROUGH tikhomirov@285: } tikhomirov@285: throw new IllegalStateException(new String(data, start, length)); tikhomirov@285: } tikhomirov@285: tikhomirov@285: String nativeString() { tikhomirov@285: if (this == Exec) { tikhomirov@285: return "x"; tikhomirov@285: } tikhomirov@285: if (this == Link) { tikhomirov@285: return "l"; tikhomirov@285: } tikhomirov@415: if (this == RegularFile) { tikhomirov@415: return ""; tikhomirov@415: } tikhomirov@285: throw new IllegalStateException(toString()); tikhomirov@285: } tikhomirov@285: } tikhomirov@2: tikhomirov@412: /*package-local*/ HgManifest(HgRepository hgRepo, RevlogStream content, EncodingHelper eh) { tikhomirov@21: super(hgRepo, content); tikhomirov@412: encodingHelper = eh; tikhomirov@13: } tikhomirov@13: tikhomirov@218: /** tikhomirov@390: * Walks manifest revisions that correspond to specified range of changesets. The order in which manifest versions get reported tikhomirov@390: * to the inspector corresponds to physical order of manifest revisions, not that of changesets (with few exceptions as noted below). tikhomirov@390: * That is, for cset-manifest revision pairs: tikhomirov@390: *
tikhomirov@390: * 3 8 tikhomirov@390: * 4 7 tikhomirov@390: * 5 9 tikhomirov@390: *tikhomirov@390: * call
walk(3,5, insp)
would yield (4,7), (3,8) and (5,9) to the inspector;
tikhomirov@390: * different order of arguments, walk(5, 3, insp)
, makes no difference.
tikhomirov@390: *
tikhomirov@390: * Physical layout of mercurial files (revlog) doesn't impose any restriction on whether manifest and changeset revisions shall go
tikhomirov@390: * incrementally, nor it mandates presence of manifest version for a changeset. Thus, there might be changesets that record {@link Nodeid#NULL}
tikhomirov@390: * as corresponding manifest revision. This situation is deemed exceptional now and what would inspector
get depends on whether
tikhomirov@390: * start
or end
arguments point to such changeset, or such changeset happen to be somewhere inside the range
tikhomirov@423: * [start..end]
. Implementation does it best to report empty manifests
tikhomirov@423: * (Inspector.begin(HgRepository.NO_REVISION, NULL, csetRevIndex);
tikhomirov@423: * followed immediately by Inspector.end(HgRepository.NO_REVISION)
tikhomirov@423: * when start
and/or end
point to changeset with no associated
tikhomirov@390: * manifest revision. However, if changeset-manifest revision pairs look like:
tikhomirov@390: *
tikhomirov@390: * 3 8 tikhomirov@390: * 4 -1 (cset records null revision for manifest) tikhomirov@390: * 5 9 tikhomirov@390: *tikhomirov@390: * call
walk(3,5, insp)
would yield only (3,8) and (5,9) to the inspector, without additional empty
tikhomirov@390: * Inspector.begin(); Inspector.end()
call pair.
tikhomirov@218: *
tikhomirov@423: * @see HgRepository#NO_REVISION
tikhomirov@218: * @param start changelog (not manifest!) revision to begin with
tikhomirov@218: * @param end changelog (not manifest!) revision to stop, inclusive.
tikhomirov@390: * @param inspector manifest revision visitor, can't be null
tikhomirov@423: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception
tikhomirov@423: * @throws IllegalArgumentException if inspector callback is null
tikhomirov@218: */
tikhomirov@423: public void walk(int start, int end, final Inspector inspector) throws HgRuntimeException, IllegalArgumentException {
tikhomirov@196: if (inspector == null) {
tikhomirov@196: throw new IllegalArgumentException();
tikhomirov@196: }
tikhomirov@390: final int csetFirst = start <= end ? start : end, csetLast = start > end ? start : end;
tikhomirov@390: int manifestFirst, manifestLast, i = 0;
tikhomirov@390: do {
tikhomirov@390: manifestFirst = fromChangelog(csetFirst+i);
tikhomirov@403: if (manifestFirst == BAD_REVISION) {
tikhomirov@423: inspector.begin(NO_REVISION, NULL, csetFirst+i);
tikhomirov@423: inspector.end(NO_REVISION);
tikhomirov@390: }
tikhomirov@390: i++;
tikhomirov@403: } while (manifestFirst == BAD_REVISION && csetFirst+i <= csetLast);
tikhomirov@403: if (manifestFirst == BAD_REVISION) {
tikhomirov@390: getRepo().getContext().getLog().info(getClass(), "None of changesets [%d..%d] have associated manifest revision", csetFirst, csetLast);
tikhomirov@390: // we ran through all revisions in [start..end] and none of them had manifest.
tikhomirov@390: // we reported that to inspector and proceeding is done now.
tikhomirov@390: return;
tikhomirov@390: }
tikhomirov@390: i = 0;
tikhomirov@390: do {
tikhomirov@390: manifestLast = fromChangelog(csetLast-i);
tikhomirov@403: if (manifestLast == BAD_REVISION) {
tikhomirov@423: inspector.begin(NO_REVISION, NULL, csetLast-i);
tikhomirov@423: inspector.end(NO_REVISION);
tikhomirov@390: }
tikhomirov@390: i++;
tikhomirov@403: } while (manifestLast == BAD_REVISION && csetLast-i >= csetFirst);
tikhomirov@403: if (manifestLast == BAD_REVISION) {
tikhomirov@423: // hmm, manifestFirst != BAD_REVISION here, hence there's i from [csetFirst..csetLast] for which manifest entry exists,
tikhomirov@423: // and thus it's impossible to run into manifestLast == BAD_REVISION. Nevertheless, never hurts to check.
tikhomirov@423: throw new HgInvalidStateException(String.format("Manifest %d-%d(!) for cset range [%d..%d] ", manifestFirst, manifestLast, csetFirst, csetLast));
tikhomirov@390: }
tikhomirov@390: if (manifestLast < manifestFirst) {
tikhomirov@300: // there are tool-constructed repositories that got order of changeset revisions completely different from that of manifest
tikhomirov@390: int x = manifestLast;
tikhomirov@390: manifestLast = manifestFirst;
tikhomirov@390: manifestFirst = x;
tikhomirov@300: }
tikhomirov@412: content.iterate(manifestFirst, manifestLast, true, new ManifestParser(inspector, encodingHelper));
tikhomirov@19: }
tikhomirov@218:
tikhomirov@254: /**
tikhomirov@390: * "Sparse" iteration of the manifest, more effective than accessing revisions one by one.
tikhomirov@390: * Inspector is invoked for each changeset revision supplied, even when there's no manifest
tikhomirov@390: * revision associated with a changeset (@see {@link #walk(int, int, Inspector)} for more details when it happens). Order inspector
tikhomirov@390: * gets invoked doesn't resemble order of changeset revisions supplied, manifest revisions are reported in the order they appear
tikhomirov@390: * in manifest revlog (with exception of changesets with missing manifest that may be reported in any order).
tikhomirov@254: *
tikhomirov@390: * @param inspector manifest revision visitor, can't be null
tikhomirov@390: * @param revisionIndexes local indexes of changesets to visit, non-null
tikhomirov@423: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception
tikhomirov@423: * @throws InvalidArgumentException if supplied arguments are null
s
tikhomirov@254: */
tikhomirov@423: public void walk(final Inspector inspector, int... revisionIndexes) throws HgRuntimeException, IllegalArgumentException {
tikhomirov@367: if (inspector == null || revisionIndexes == null) {
tikhomirov@254: throw new IllegalArgumentException();
tikhomirov@254: }
tikhomirov@390: int[] manifestRevs = toManifestRevisionIndexes(revisionIndexes, inspector);
tikhomirov@412: content.iterate(manifestRevs, true, new ManifestParser(inspector, encodingHelper));
tikhomirov@254: }
tikhomirov@254:
tikhomirov@390: //
tikhomirov@390: /**
tikhomirov@403: * Tells manifest revision number that corresponds to the given changeset. May return {@link HgRepository#BAD_REVISION}
tikhomirov@403: * if changeset has no associated manifest (cset records NULL nodeid for manifest).
tikhomirov@403: * @return manifest revision index, non-negative, or {@link HgRepository#BAD_REVISION}.
tikhomirov@403: * @throws HgInvalidRevisionException if method argument specifies non-existent revision index
tikhomirov@403: * @throws IllegalArgumentException if argument is not a revision index
tikhomirov@403: * @throws HgInvalidControlFileException if access to revlog index/data entry failed
tikhomirov@390: */
tikhomirov@403: /*package-local*/ int fromChangelog(int changesetRevisionIndex) throws HgInvalidRevisionException, HgInvalidControlFileException {
tikhomirov@368: if (HgInternals.wrongRevisionIndex(changesetRevisionIndex)) {
tikhomirov@368: throw new IllegalArgumentException(String.valueOf(changesetRevisionIndex));
tikhomirov@218: }
tikhomirov@368: if (changesetRevisionIndex == HgRepository.WORKING_COPY || changesetRevisionIndex == HgRepository.BAD_REVISION) {
tikhomirov@257: throw new IllegalArgumentException("Can't use constants like WORKING_COPY or BAD_REVISION");
tikhomirov@257: }
tikhomirov@257: // revisionNumber == TIP is processed by RevisionMapper
tikhomirov@218: if (revisionMap == null) {
tikhomirov@218: revisionMap = new RevisionMapper(getRepo());
tikhomirov@218: content.iterate(0, TIP, false, revisionMap);
tikhomirov@218: }
tikhomirov@368: return revisionMap.at(changesetRevisionIndex);
tikhomirov@218: }
tikhomirov@218:
tikhomirov@232: /**
tikhomirov@232: * Extracts file revision as it was known at the time of given changeset.
tikhomirov@417: * For more thorough details about file at specific changeset, use {@link HgChangesetFileSneaker}.
tikhomirov@232: *
tikhomirov@417: * @see HgChangesetFileSneaker
tikhomirov@367: * @param changelogRevisionIndex local changeset index
tikhomirov@232: * @param file path to file in question
tikhomirov@232: * @return file revision or null
if manifest at specified revision doesn't list such file
tikhomirov@403: * @throws HgInvalidRevisionException if method argument specifies non-existent revision index
tikhomirov@403: * @throws HgInvalidControlFileException if access to revlog index/data entry failed
tikhomirov@232: */
tikhomirov@403: public Nodeid getFileRevision(int changelogRevisionIndex, final Path file) throws HgInvalidRevisionException, HgInvalidControlFileException {
tikhomirov@417: // there's no need for HgDataFile to own this method, or get a delegate
tikhomirov@417: // as most of HgDataFile API is using file revision indexes, and there's easy step from file revision index to
tikhomirov@417: // both file revision and changeset revision index. But there's no easy way to go from changesetRevisionIndex to
tikhomirov@417: // file revision (the task this method solves), exept for HgFileInformer
tikhomirov@417: // I feel methods dealing with changeset indexes shall be more exposed in HgChangelog and HgManifest API.
tikhomirov@367: return getFileRevisions(file, changelogRevisionIndex).get(changelogRevisionIndex);
tikhomirov@326: }
tikhomirov@417:
tikhomirov@417: // XXX package-local or better API
tikhomirov@417: @Experimental(reason="Map as return value isn't that good")
tikhomirov@403: public Mapnull
if file was not known in the specified revision
tikhomirov@415: * FIXME EXCEPTIONS
tikhomirov@415: * @throws HgInvalidControlFileException
tikhomirov@415: * @throws HgInvalidRevisionException
tikhomirov@415: */
tikhomirov@417: public Flags getFileFlags(int changesetRevIndex, Path file) throws HgInvalidRevisionException, HgInvalidControlFileException {
tikhomirov@415: int manifestRevIdx = fromChangelog(changesetRevIndex);
tikhomirov@415: IntMapnull
tikhomirov@415: * @return true
to continue iteration, false
to stop
tikhomirov@415: */
tikhomirov@285: boolean next(Nodeid nid, Path fname, Flags flags);
tikhomirov@424: boolean end(int manifestRevision);
tikhomirov@267: }
tikhomirov@424:
tikhomirov@268: /**
tikhomirov@268: * When Pool uses Strings directly,
tikhomirov@268: * ManifestParser creates new String instance with new char[] value, and does byte->char conversion.
tikhomirov@268: * For cpython repo, walk(0..10k), there are over 16 million filenames, of them only 3020 unique.
tikhomirov@268: * This means there are 15.9 million useless char[] instances and byte->char conversions
tikhomirov@268: *
tikhomirov@285: * When String (Path) is wrapped into {@link PathProxy}, there's extra overhead of byte[] representation
tikhomirov@285: * of the String, but these are only for unique Strings (Paths) (3020 in the example above). Besides, I save
tikhomirov@268: * useless char[] and byte->char conversions.
tikhomirov@268: */
tikhomirov@285: private static class PathProxy {
tikhomirov@267: private byte[] data;
tikhomirov@268: private int start;
tikhomirov@268: private final int hash, length;
tikhomirov@285: private Path result;
tikhomirov@412: private final EncodingHelper encHelper;
tikhomirov@267:
tikhomirov@412: public PathProxy(byte[] data, int start, int length, EncodingHelper eh) {
tikhomirov@267: this.data = data;
tikhomirov@267: this.start = start;
tikhomirov@267: this.length = length;
tikhomirov@412: this.encHelper = eh;
tikhomirov@267:
tikhomirov@268: // copy from String.hashCode(). In fact, not necessarily match result of String(data).hashCode
tikhomirov@268: // just need some nice algorithm here
tikhomirov@267: int h = 0;
tikhomirov@267: byte[] d = data;
tikhomirov@267: for (int i = 0, off = start, len = length; i < len; i++) {
tikhomirov@267: h = 31 * h + d[off++];
tikhomirov@267: }
tikhomirov@267: hash = h;
tikhomirov@267: }
tikhomirov@267:
tikhomirov@267: @Override
tikhomirov@267: public boolean equals(Object obj) {
tikhomirov@285: if (false == obj instanceof PathProxy) {
tikhomirov@267: return false;
tikhomirov@267: }
tikhomirov@285: PathProxy o = (PathProxy) obj;
tikhomirov@267: if (o.result != null && result != null) {
tikhomirov@267: return result.equals(o.result);
tikhomirov@267: }
tikhomirov@268: if (o.length != length || o.hash != hash) {
tikhomirov@267: return false;
tikhomirov@267: }
tikhomirov@267: for (int i = 0, x = o.start, y = start; i < length; i++) {
tikhomirov@267: if (o.data[x++] != data[y++]) {
tikhomirov@267: return false;
tikhomirov@267: }
tikhomirov@267: }
tikhomirov@267: return true;
tikhomirov@267: }
tikhomirov@267: @Override
tikhomirov@267: public int hashCode() {
tikhomirov@267: return hash;
tikhomirov@267: }
tikhomirov@267:
tikhomirov@285: public Path freeze() {
tikhomirov@267: if (result == null) {
tikhomirov@412: result = Path.create(encHelper.fromManifest(data, start, length));
tikhomirov@268: // release reference to bigger data array, make a copy of relevant part only
tikhomirov@285: // use original bytes, not those from String above to avoid cache misses due to different encodings
tikhomirov@268: byte[] d = new byte[length];
tikhomirov@268: System.arraycopy(data, start, d, 0, length);
tikhomirov@268: data = d;
tikhomirov@268: start = 0;
tikhomirov@267: }
tikhomirov@267: return result;
tikhomirov@267: }
tikhomirov@267: }
tikhomirov@196:
tikhomirov@312: private static class ManifestParser implements RevlogStream.Inspector, Lifecycle {
tikhomirov@196: private final Inspector inspector;
tikhomirov@304: private Pool2