tikhomirov@17: /* tikhomirov@603: * 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@2: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@2: tikhomirov@367: import static org.tmatesoft.hg.repo.HgInternals.wrongRevisionIndex; tikhomirov@148: import static org.tmatesoft.hg.repo.HgRepository.*; tikhomirov@602: import static org.tmatesoft.hg.util.LogFacility.Severity.Info; tikhomirov@74: tikhomirov@237: import java.io.File; tikhomirov@237: import java.io.FileInputStream; tikhomirov@148: import java.io.IOException; tikhomirov@115: import java.nio.ByteBuffer; tikhomirov@237: import java.nio.channels.FileChannel; tikhomirov@240: import java.util.Arrays; tikhomirov@78: tikhomirov@425: import org.tmatesoft.hg.core.HgChangesetFileSneaker; tikhomirov@689: import org.tmatesoft.hg.core.HgFileRevision; tikhomirov@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@157: import org.tmatesoft.hg.internal.DataAccess; tikhomirov@619: import org.tmatesoft.hg.internal.FileUtils; tikhomirov@121: import org.tmatesoft.hg.internal.FilterByteChannel; tikhomirov@277: import org.tmatesoft.hg.internal.FilterDataAccess; tikhomirov@420: import org.tmatesoft.hg.internal.Internals; tikhomirov@602: import org.tmatesoft.hg.internal.Metadata; tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream; tikhomirov@115: import org.tmatesoft.hg.util.ByteChannel; tikhomirov@237: import org.tmatesoft.hg.util.CancelSupport; tikhomirov@148: import org.tmatesoft.hg.util.CancelledException; tikhomirov@388: import org.tmatesoft.hg.util.LogFacility; tikhomirov@305: import org.tmatesoft.hg.util.Pair; tikhomirov@133: import org.tmatesoft.hg.util.Path; tikhomirov@237: import org.tmatesoft.hg.util.ProgressSupport; tikhomirov@74: tikhomirov@5: tikhomirov@2: /** tikhomirov@425: * Regular user data file stored in the repository. tikhomirov@425: * tikhomirov@425: *

Note, most methods accept index in the file's revision history, not that of changelog. Easy way to obtain tikhomirov@425: * changeset revision index from file's is to use {@link #getChangesetRevisionIndex(int)}. To obtain file's revision tikhomirov@425: * index for a given changeset, {@link HgManifest#getFileRevision(int, Path)} or {@link HgChangesetFileSneaker} may tikhomirov@425: * come handy. tikhomirov@74: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@2: */ tikhomirov@426: public final class HgDataFile extends Revlog { tikhomirov@2: tikhomirov@3: // absolute from repo root? tikhomirov@3: // slashes, unix-style? tikhomirov@3: // repo location agnostic, just to give info to user, not to access real storage tikhomirov@74: private final Path path; tikhomirov@694: /* tikhomirov@694: * Get initialized on first access to file content. tikhomirov@694: * We read metadata starting from rev 0 always, so that Metadata#lastRevisionRead() tikhomirov@694: * shows the region of file history [0..lastRevisionRead] we know metadata for tikhomirov@694: */ tikhomirov@694: private Metadata metadata; tikhomirov@2: tikhomirov@115: /*package-local*/HgDataFile(HgRepository hgRepo, Path filePath, RevlogStream content) { tikhomirov@600: super(hgRepo, content, false); tikhomirov@115: path = filePath; tikhomirov@3: } tikhomirov@115: tikhomirov@115: // exists is not the best name possible. now it means no file with such name was ever known to the repo. tikhomirov@426: // it might be confused with files existed before but lately removed. TODO HgFileNode.exists makes more sense. tikhomirov@426: // or HgDataFile.known() tikhomirov@3: public boolean exists() { tikhomirov@621: return content.exists(); tikhomirov@2: } tikhomirov@2: tikhomirov@426: /** tikhomirov@426: * Human-readable file name, i.e. "COPYING", not "store/data/_c_o_p_y_i_n_g.i" tikhomirov@426: */ tikhomirov@74: public Path getPath() { tikhomirov@157: return path; // hgRepo.backresolve(this) -> name? In this case, what about hashed long names? tikhomirov@2: } tikhomirov@2: tikhomirov@275: /** tikhomirov@416: * Handy shorthand for {@link #getLength(int) length(getRevisionIndex(nodeid))} tikhomirov@354: * tikhomirov@354: * @param nodeid revision of the file tikhomirov@354: * tikhomirov@275: * @return size of the file content at the given revision tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@275: */ tikhomirov@425: public int getLength(Nodeid nodeid) throws HgRuntimeException { tikhomirov@396: try { tikhomirov@416: return getLength(getRevisionIndex(nodeid)); tikhomirov@396: } catch (HgInvalidControlFileException ex) { tikhomirov@396: throw ex.isRevisionSet() ? ex : ex.setRevision(nodeid); tikhomirov@396: } catch (HgInvalidRevisionException ex) { tikhomirov@396: throw ex.isRevisionSet() ? ex : ex.setRevision(nodeid); tikhomirov@396: } tikhomirov@275: } tikhomirov@275: tikhomirov@275: /** tikhomirov@368: * @param fileRevisionIndex - revision local index, non-negative. From predefined constants, only {@link HgRepository#TIP} makes sense. tikhomirov@275: * @return size of the file content at the revision identified by local revision number. tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@275: */ tikhomirov@425: public int getLength(int fileRevisionIndex) throws HgRuntimeException { tikhomirov@401: if (wrongRevisionIndex(fileRevisionIndex) || fileRevisionIndex == BAD_REVISION) { tikhomirov@401: throw new HgInvalidRevisionException(fileRevisionIndex); tikhomirov@401: } tikhomirov@401: if (fileRevisionIndex == TIP) { tikhomirov@401: fileRevisionIndex = getLastRevision(); tikhomirov@401: } else if (fileRevisionIndex == WORKING_COPY) { tikhomirov@401: File f = getRepo().getFile(this); tikhomirov@401: if (f.exists()) { tikhomirov@420: // single revision can't be greater than 2^32, shall be safe to cast to int tikhomirov@420: return Internals.ltoi(f.length()); tikhomirov@401: } tikhomirov@401: Nodeid fileRev = getWorkingCopyRevision(); tikhomirov@401: if (fileRev == null) { tikhomirov@401: throw new HgInvalidRevisionException(String.format("File %s is not part of working copy", getPath()), null, fileRevisionIndex); tikhomirov@401: } tikhomirov@401: fileRevisionIndex = getRevisionIndex(fileRev); tikhomirov@401: } tikhomirov@367: if (metadata == null || !metadata.checked(fileRevisionIndex)) { tikhomirov@367: checkAndRecordMetadata(fileRevisionIndex); tikhomirov@275: } tikhomirov@367: final int dataLen = content.dataLength(fileRevisionIndex); tikhomirov@367: if (metadata.known(fileRevisionIndex)) { tikhomirov@367: return dataLen - metadata.dataOffset(fileRevisionIndex); tikhomirov@275: } tikhomirov@275: return dataLen; tikhomirov@22: } tikhomirov@416: tikhomirov@416: /** tikhomirov@237: * Reads content of the file from working directory. If file present in the working directory, its actual content without tikhomirov@237: * any filters is supplied through the sink. If file does not exist in the working dir, this method provides content of a file tikhomirov@401: * as if it would be refreshed in the working copy, i.e. its corresponding revision (according to dirstate) is read from the tikhomirov@401: * repository, and filters repo -> working copy get applied. tikhomirov@401: * tikhomirov@401: * NOTE, if file is missing from the working directory and is not part of the dirstate (but otherwise legal repository file, tikhomirov@401: * e.g. from another branch), no content would be supplied. tikhomirov@237: * tikhomirov@396: * @param sink content consumer tikhomirov@380: * @throws CancelledException if execution of the operation was cancelled tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@237: */ tikhomirov@425: public void workingCopy(ByteChannel sink) throws CancelledException, HgRuntimeException { tikhomirov@237: File f = getRepo().getFile(this); tikhomirov@237: if (f.exists()) { tikhomirov@237: final CancelSupport cs = CancelSupport.Factory.get(sink); tikhomirov@237: final ProgressSupport progress = ProgressSupport.Factory.get(sink); tikhomirov@237: final long flength = f.length(); tikhomirov@237: final int bsize = (int) Math.min(flength, 32*1024); tikhomirov@237: progress.start((int) (flength > Integer.MAX_VALUE ? flength >>> 15 /*32 kb buf size*/ : flength)); tikhomirov@237: ByteBuffer buf = ByteBuffer.allocate(bsize); tikhomirov@619: FileInputStream fis = null; tikhomirov@237: try { tikhomirov@619: fis = new FileInputStream(f); tikhomirov@619: FileChannel fc = fis.getChannel(); tikhomirov@237: while (fc.read(buf) != -1) { tikhomirov@237: cs.checkCancelled(); tikhomirov@237: buf.flip(); tikhomirov@237: int consumed = sink.write(buf); tikhomirov@237: progress.worked(flength > Integer.MAX_VALUE ? 1 : consumed); tikhomirov@237: buf.compact(); tikhomirov@237: } tikhomirov@237: } catch (IOException ex) { tikhomirov@396: throw new HgInvalidFileException("Working copy read failed", ex, f); tikhomirov@237: } finally { tikhomirov@237: progress.done(); tikhomirov@619: if (fis != null) { tikhomirov@654: new FileUtils(getRepo().getSessionContext().getLog(), this).closeQuietly(fis); tikhomirov@237: } tikhomirov@237: } tikhomirov@237: } else { tikhomirov@401: Nodeid fileRev = getWorkingCopyRevision(); tikhomirov@401: if (fileRev == null) { tikhomirov@401: // no content for this data file in the working copy - it is not part of the actual working state. tikhomirov@401: // XXX perhaps, shall report this to caller somehow, not silently pass no data? tikhomirov@388: return; tikhomirov@388: } tikhomirov@401: final int fileRevIndex = getRevisionIndex(fileRev); tikhomirov@401: contentWithFilters(fileRevIndex, sink); tikhomirov@401: } tikhomirov@401: } tikhomirov@401: tikhomirov@401: /** tikhomirov@401: * @return file revision as recorded in repository manifest for dirstate parent, or null if no file revision can be found 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@401: */ tikhomirov@628: private Nodeid getWorkingCopyRevision() throws HgRuntimeException { tikhomirov@401: final Pair wcParents = getRepo().getWorkingCopyParents(); tikhomirov@401: Nodeid p = wcParents.first().isNull() ? wcParents.second() : wcParents.first(); tikhomirov@401: final HgChangelog clog = getRepo().getChangelog(); tikhomirov@401: final int csetRevIndex; tikhomirov@401: if (p.isNull()) { tikhomirov@401: // no dirstate parents tikhomirov@490: getRepo().getSessionContext().getLog().dump(getClass(), Info, "No dirstate parents, resort to TIP", getPath()); tikhomirov@401: // if it's a repository with no dirstate, use TIP then tikhomirov@401: csetRevIndex = clog.getLastRevision(); tikhomirov@401: if (csetRevIndex == -1) { tikhomirov@401: // shall not happen provided there's .i for this data file (hence at least one cset) tikhomirov@401: // and perhaps exception is better here. However, null as "can't find" indication seems reasonable. tikhomirov@401: return null; tikhomirov@401: } tikhomirov@401: } else { tikhomirov@388: // common case to avoid searching complete changelog for nodeid match tikhomirov@388: final Nodeid tipRev = clog.getRevision(TIP); tikhomirov@388: if (tipRev.equals(p)) { tikhomirov@388: csetRevIndex = clog.getLastRevision(); tikhomirov@388: } else { tikhomirov@388: // bad luck, need to search honestly tikhomirov@628: csetRevIndex = clog.getRevisionIndex(p); tikhomirov@388: } tikhomirov@237: } tikhomirov@401: Nodeid fileRev = getRepo().getManifest().getFileRevision(csetRevIndex, getPath()); tikhomirov@401: // it's possible for a file to be in working dir and have store/.i but to belong e.g. to a different tikhomirov@401: // branch than the one from dirstate. Thus it's possible to get null fileRev tikhomirov@401: // which would serve as an indication this data file is not part of working copy tikhomirov@401: return fileRev; tikhomirov@2: } tikhomirov@115: tikhomirov@396: /** tikhomirov@396: * Access content of a file revision tikhomirov@396: * XXX not sure distinct method contentWithFilters() is the best way to do, perhaps, callers shall add filters themselves? tikhomirov@396: * tikhomirov@396: * @param fileRevisionIndex - revision local index, non-negative. From predefined constants, {@link HgRepository#TIP} and {@link HgRepository#WORKING_COPY} make sense. tikhomirov@396: * @param sink content consumer tikhomirov@396: * tikhomirov@396: * @throws CancelledException if execution of the operation was cancelled tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@396: */ tikhomirov@425: public void contentWithFilters(int fileRevisionIndex, ByteChannel sink) throws CancelledException, HgRuntimeException { tikhomirov@396: if (fileRevisionIndex == WORKING_COPY) { tikhomirov@237: workingCopy(sink); // pass un-mangled sink tikhomirov@237: } else { tikhomirov@396: content(fileRevisionIndex, new FilterByteChannel(sink, getRepo().getFiltersFromRepoToWorkingDir(getPath()))); tikhomirov@237: } tikhomirov@115: } tikhomirov@22: tikhomirov@367: /** tikhomirov@396: * Retrieve content of specific revision. Content is provided as is, without any filters (e.g. keywords, eol, etc.) applied. tikhomirov@396: * For filtered content, use {@link #contentWithFilters(int, ByteChannel)}. tikhomirov@367: * tikhomirov@368: * @param fileRevisionIndex - revision local index, non-negative. From predefined constants, {@link HgRepository#TIP} and {@link HgRepository#WORKING_COPY} make sense. tikhomirov@396: * @param sink content consumer tikhomirov@396: * tikhomirov@380: * @throws CancelledException if execution of the operation was cancelled tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@367: */ tikhomirov@425: public void content(int fileRevisionIndex, ByteChannel sink) throws CancelledException, HgRuntimeException { tikhomirov@367: // for data files need to check heading of the file content for possible metadata tikhomirov@367: // @see http://mercurial.selenic.com/wiki/FileFormats#data.2BAC8- tikhomirov@367: if (fileRevisionIndex == TIP) { tikhomirov@367: fileRevisionIndex = getLastRevision(); tikhomirov@78: } tikhomirov@367: if (fileRevisionIndex == WORKING_COPY) { tikhomirov@237: // sink is supposed to come into workingCopy without filters tikhomirov@237: // thus we shall not get here (into #content) from #contentWithFilters(WC) tikhomirov@157: workingCopy(sink); tikhomirov@157: return; tikhomirov@157: } tikhomirov@367: if (wrongRevisionIndex(fileRevisionIndex) || fileRevisionIndex == BAD_REVISION) { tikhomirov@367: throw new HgInvalidRevisionException(fileRevisionIndex); tikhomirov@148: } tikhomirov@157: if (sink == null) { tikhomirov@157: throw new IllegalArgumentException(); tikhomirov@157: } tikhomirov@134: if (metadata == null) { tikhomirov@602: metadata = new Metadata(getRepo()); tikhomirov@134: } tikhomirov@277: ErrorHandlingInspector insp; tikhomirov@490: final LogFacility lf = getRepo().getSessionContext().getLog(); tikhomirov@367: if (metadata.none(fileRevisionIndex)) { tikhomirov@388: insp = new ContentPipe(sink, 0, lf); tikhomirov@367: } else if (metadata.known(fileRevisionIndex)) { tikhomirov@388: insp = new ContentPipe(sink, metadata.dataOffset(fileRevisionIndex), lf); tikhomirov@157: } else { tikhomirov@157: // do not know if there's metadata tikhomirov@602: insp = new MetadataInspector(metadata, new ContentPipe(sink, 0, lf)); tikhomirov@78: } tikhomirov@157: insp.checkCancelled(); tikhomirov@367: super.content.iterate(fileRevisionIndex, fileRevisionIndex, true, insp); tikhomirov@157: try { tikhomirov@425: insp.checkFailed(); tikhomirov@396: } catch (HgInvalidControlFileException ex) { tikhomirov@396: ex = ex.setFileName(getPath()); tikhomirov@396: throw ex.isRevisionIndexSet() ? ex : ex.setRevisionIndex(fileRevisionIndex); tikhomirov@237: } catch (IOException ex) { tikhomirov@396: HgInvalidControlFileException e = new HgInvalidControlFileException("Revision content access failed", ex, null); tikhomirov@396: throw content.initWithIndexFile(e).setFileName(getPath()).setRevisionIndex(fileRevisionIndex); tikhomirov@78: } tikhomirov@78: } tikhomirov@425: tikhomirov@425: /** tikhomirov@425: * Walk complete change history of the file. tikhomirov@425: * @param inspector callback to visit changesets tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@425: */ tikhomirov@425: public void history(HgChangelog.Inspector inspector) throws HgRuntimeException { tikhomirov@135: history(0, getLastRevision(), inspector); tikhomirov@48: } tikhomirov@48: tikhomirov@417: /** tikhomirov@425: * Walk subset of the file's change history. tikhomirov@425: * @param start revision local index, inclusive; non-negative or {@link HgRepository#TIP} tikhomirov@425: * @param end revision local index, inclusive; non-negative or {@link HgRepository#TIP} tikhomirov@425: * @param inspector callback to visit changesets tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@417: */ tikhomirov@425: public void history(int start, int end, HgChangelog.Inspector inspector) throws HgRuntimeException { tikhomirov@3: if (!exists()) { tikhomirov@3: throw new IllegalStateException("Can't get history of invalid repository file node"); tikhomirov@3: } tikhomirov@135: final int last = getLastRevision(); tikhomirov@77: if (end == TIP) { tikhomirov@77: end = last; tikhomirov@77: } tikhomirov@300: if (start == TIP) { tikhomirov@300: start = last; tikhomirov@300: } tikhomirov@300: HgInternals.checkRevlogRange(start, end, last); tikhomirov@300: tikhomirov@48: final int[] commitRevisions = new int[end - start + 1]; tikhomirov@242: final boolean[] needsSorting = { false }; tikhomirov@77: RevlogStream.Inspector insp = new RevlogStream.Inspector() { tikhomirov@3: int count = 0; tikhomirov@51: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess data) { tikhomirov@242: if (count > 0) { tikhomirov@242: if (commitRevisions[count -1] > linkRevision) { tikhomirov@242: needsSorting[0] = true; tikhomirov@242: } tikhomirov@242: } tikhomirov@3: commitRevisions[count++] = linkRevision; tikhomirov@3: } tikhomirov@3: }; tikhomirov@48: content.iterate(start, end, false, insp); tikhomirov@233: final HgChangelog changelog = getRepo().getChangelog(); tikhomirov@242: if (needsSorting[0]) { tikhomirov@242: // automatic tools (svnmerge?) produce unnatural file history tikhomirov@242: // (e.g. cpython/Lib/doctest.py, revision 164 points to cset 63509, 165 - to 38453) tikhomirov@242: Arrays.sort(commitRevisions); tikhomirov@233: } tikhomirov@245: changelog.rangeInternal(inspector, commitRevisions); tikhomirov@3: } tikhomirov@88: tikhomirov@354: /** tikhomirov@367: * For a given revision of the file (identified with revision index), find out index of the corresponding changeset. tikhomirov@354: * tikhomirov@354: * @return changeset revision index tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@354: */ tikhomirov@437: public int getChangesetRevisionIndex(int fileRevisionIndex) throws HgRuntimeException { tikhomirov@437: return content.linkRevision(fileRevisionIndex); tikhomirov@367: } tikhomirov@88: tikhomirov@354: /** tikhomirov@367: * Complements {@link #getChangesetRevisionIndex(int)} to get changeset revision that corresponds to supplied file revision tikhomirov@354: * tikhomirov@354: * @param nid revision of the file tikhomirov@354: * @return changeset revision tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@354: */ tikhomirov@425: public Nodeid getChangesetRevision(Nodeid nid) throws HgRuntimeException { tikhomirov@367: int changelogRevision = getChangesetRevisionIndex(getRevisionIndex(nid)); tikhomirov@88: return getRepo().getChangelog().getRevision(changelogRevision); tikhomirov@88: } tikhomirov@78: tikhomirov@354: /** tikhomirov@689: * Tells whether first revision of this file originates from another repository file. tikhomirov@689: * This method is shorthand for {@link #isCopy(int) isCopy(0)} and it's advised to use {@link #isCopy(int)} instead. tikhomirov@689: * tikhomirov@689: * @return true if first revision of this file is a copy of some other from the repository tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@354: */ tikhomirov@425: public boolean isCopy() throws HgRuntimeException { tikhomirov@689: return isCopy(0); tikhomirov@78: } tikhomirov@78: tikhomirov@354: /** tikhomirov@689: * Get name of the file first revision of this one was copied from. tikhomirov@689: * Note, it's better to use {@link #getCopySource(int)} instead. tikhomirov@354: * tikhomirov@354: * @return name of the file origin tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@354: */ tikhomirov@425: public Path getCopySourceName() throws HgRuntimeException { tikhomirov@78: if (isCopy()) { tikhomirov@571: Path.Source ps = getRepo().getSessionContext().getPathFactory(); tikhomirov@571: return ps.path(metadata.find(0, "copy")); tikhomirov@78: } tikhomirov@78: throw new UnsupportedOperationException(); // XXX REVISIT, think over if Exception is good (clients would check isCopy() anyway, perhaps null is sufficient?) tikhomirov@78: } tikhomirov@78: tikhomirov@415: /** tikhomirov@689: * Use {@link #getCopySource(int)} instead tikhomirov@415: * @return revision this file was copied from tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@415: */ tikhomirov@425: public Nodeid getCopySourceRevision() throws HgRuntimeException { tikhomirov@78: if (isCopy()) { tikhomirov@78: return Nodeid.fromAscii(metadata.find(0, "copyrev")); // XXX reuse/cache Nodeid tikhomirov@78: } tikhomirov@78: throw new UnsupportedOperationException(); tikhomirov@78: } tikhomirov@689: tikhomirov@689: /** tikhomirov@689: * Tell if specified file revision was created by copying or renaming another file tikhomirov@689: * tikhomirov@689: * @param fileRevisionIndex index of file revision to check tikhomirov@689: * @return true if this revision originates (as a result of copy/rename) from another file tikhomirov@689: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@689: * @since 1.2 tikhomirov@689: */ tikhomirov@689: public boolean isCopy(int fileRevisionIndex) throws HgRuntimeException { tikhomirov@689: if (fileRevisionIndex == TIP) { tikhomirov@689: fileRevisionIndex = getLastRevision(); tikhomirov@689: } tikhomirov@689: if (wrongRevisionIndex(fileRevisionIndex) || fileRevisionIndex == BAD_REVISION || fileRevisionIndex == WORKING_COPY || fileRevisionIndex == NO_REVISION) { tikhomirov@689: throw new HgInvalidRevisionException(fileRevisionIndex); tikhomirov@689: } tikhomirov@689: tikhomirov@689: if (metadata == null || !metadata.checked(fileRevisionIndex)) { tikhomirov@689: checkAndRecordMetadata(fileRevisionIndex); tikhomirov@689: } tikhomirov@689: if (!metadata.known(fileRevisionIndex)) { tikhomirov@689: return false; tikhomirov@689: } tikhomirov@689: return metadata.find(fileRevisionIndex, "copy") != null; tikhomirov@689: } tikhomirov@689: tikhomirov@689: /** tikhomirov@689: * Find out which file and which revision of that file given revision originates from tikhomirov@689: * tikhomirov@689: * @param fileRevisionIndex file revision index of interest, it's assumed {@link #isCopy(int)} for the same revision is true tikhomirov@689: * @return origin revision descriptor tikhomirov@689: * @throws HgRuntimeException tikhomirov@689: * @throws UnsupportedOperationException if specified revision is not a {@link #isCopy(int) copy} revision tikhomirov@689: * @since 1.2 tikhomirov@689: */ tikhomirov@689: public HgFileRevision getCopySource(int fileRevisionIndex) throws HgRuntimeException { tikhomirov@689: if (fileRevisionIndex == TIP) { tikhomirov@689: fileRevisionIndex = getLastRevision(); tikhomirov@689: } tikhomirov@689: if (!isCopy(fileRevisionIndex)) { tikhomirov@689: throw new UnsupportedOperationException(); tikhomirov@689: } tikhomirov@689: Path.Source ps = getRepo().getSessionContext().getPathFactory(); tikhomirov@689: Path origin = ps.path(metadata.find(fileRevisionIndex, "copy")); tikhomirov@689: Nodeid originRev = Nodeid.fromAscii(metadata.find(fileRevisionIndex, "copyrev")); // XXX reuse/cache Nodeid tikhomirov@689: return new HgFileRevision(getRepo(), originRev, null, origin); tikhomirov@689: } tikhomirov@417: tikhomirov@415: /** tikhomirov@417: * Get file flags recorded in the manifest tikhomirov@416: * @param fileRevisionIndex - revision local index, non-negative, or {@link HgRepository#TIP}. tikhomirov@417: * @see HgManifest#getFileFlags(int, Path) tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@415: */ tikhomirov@425: public HgManifest.Flags getFlags(int fileRevisionIndex) throws HgRuntimeException { tikhomirov@415: int changesetRevIndex = getChangesetRevisionIndex(fileRevisionIndex); tikhomirov@417: return getRepo().getManifest().getFileFlags(changesetRevIndex, getPath()); tikhomirov@415: } tikhomirov@629: tikhomirov@88: @Override tikhomirov@88: public String toString() { tikhomirov@88: StringBuilder sb = new StringBuilder(getClass().getSimpleName()); tikhomirov@88: sb.append('('); tikhomirov@88: sb.append(getPath()); tikhomirov@88: sb.append(')'); tikhomirov@88: return sb.toString(); tikhomirov@88: } tikhomirov@275: tikhomirov@628: private void checkAndRecordMetadata(int localRev) throws HgRuntimeException { tikhomirov@694: int startRev; tikhomirov@495: if (metadata == null) { tikhomirov@602: metadata = new Metadata(getRepo()); tikhomirov@694: startRev = 0; // read from the very beginning with one shot - likely isCopy(localRev-i) will be of interest, too tikhomirov@694: } else { tikhomirov@694: startRev = metadata.lastRevisionRead() + 1; tikhomirov@275: } tikhomirov@694: assert localRev >= startRev; // callers of this method ensure that metadata has been checked beforehand tikhomirov@495: // use MetadataInspector without delegate to process metadata only tikhomirov@602: RevlogStream.Inspector insp = new MetadataInspector(metadata, null); tikhomirov@694: super.content.iterate(startRev, localRev, true, insp); tikhomirov@275: } tikhomirov@78: tikhomirov@277: private static class MetadataInspector extends ErrorHandlingInspector implements RevlogStream.Inspector { tikhomirov@157: private final Metadata metadata; tikhomirov@277: private final RevlogStream.Inspector delegate; tikhomirov@157: tikhomirov@495: /** tikhomirov@495: * @param _metadata never null tikhomirov@495: * @param chain null if no further data processing other than metadata is desired tikhomirov@495: */ tikhomirov@602: public MetadataInspector(Metadata _metadata, RevlogStream.Inspector chain) { tikhomirov@157: metadata = _metadata; tikhomirov@277: delegate = chain; tikhomirov@277: setCancelSupport(CancelSupport.Factory.get(chain)); tikhomirov@157: } tikhomirov@157: tikhomirov@628: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess data) throws HgRuntimeException { tikhomirov@277: try { tikhomirov@694: final boolean gotMetadata = metadata.tryRead(revisionNumber, data); tikhomirov@694: if (delegate != null) { tikhomirov@277: data.reset(); tikhomirov@694: if (gotMetadata) { tikhomirov@694: // da is in prepared state (i.e. we consumed all bytes up to metadata end). tikhomirov@694: // However, it's not safe to assume delegate won't call da.reset() for some reason, tikhomirov@694: // and we need to ensure predictable result. tikhomirov@694: int offset = metadata.dataOffset(revisionNumber); tikhomirov@694: data = new FilterDataAccess(data, offset, data.length() - offset); tikhomirov@694: } tikhomirov@277: delegate.next(revisionNumber, actualLen, baseRevision, linkRevision, parent1Revision, parent2Revision, nodeid, data); tikhomirov@277: } tikhomirov@277: } catch (IOException ex) { tikhomirov@277: recordFailure(ex); tikhomirov@396: } catch (HgInvalidControlFileException ex) { tikhomirov@396: recordFailure(ex.isRevisionIndexSet() ? ex : ex.setRevisionIndex(revisionNumber)); tikhomirov@157: } tikhomirov@277: } tikhomirov@277: tikhomirov@322: @Override tikhomirov@425: public void checkFailed() throws HgRuntimeException, IOException, CancelledException { tikhomirov@322: super.checkFailed(); tikhomirov@322: if (delegate instanceof ErrorHandlingInspector) { tikhomirov@425: // TODO need to add ErrorDestination (ErrorTarget/Acceptor?) and pass it around (much like CancelSupport get passed) tikhomirov@322: // so that delegate would be able report its failures directly to caller without this hack tikhomirov@322: ((ErrorHandlingInspector) delegate).checkFailed(); tikhomirov@322: } tikhomirov@322: } tikhomirov@17: } tikhomirov@2: }