tikhomirov@17: /* tikhomirov@21: * Copyright (c) 2010, 2011 Artem Tikhomirov tikhomirov@2: */ tikhomirov@2: package com.tmate.hgkit.ll; tikhomirov@2: tikhomirov@5: import static com.tmate.hgkit.ll.HgRepository.TIP; tikhomirov@5: tikhomirov@17: import java.util.Arrays; tikhomirov@17: tikhomirov@2: /** tikhomirov@2: * Extends Revlog/uses RevlogStream? tikhomirov@2: * ? name:HgFileNode? tikhomirov@2: * @author artem tikhomirov@2: */ tikhomirov@2: public 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@2: private final String path; tikhomirov@2: tikhomirov@3: /*package-local*/HgDataFile(HgRepository hgRepo, String path, RevlogStream content) { tikhomirov@21: super(hgRepo, content); tikhomirov@3: this.path = path; tikhomirov@3: } tikhomirov@3: tikhomirov@3: public boolean exists() { tikhomirov@3: return content != null; // XXX need better impl tikhomirov@2: } tikhomirov@2: tikhomirov@2: public String getPath() { tikhomirov@2: return path; // hgRepo.backresolve(this) -> name? tikhomirov@2: } tikhomirov@2: tikhomirov@22: public int length(Nodeid nodeid) { tikhomirov@49: return content.dataLength(getLocalRevisionNumber(nodeid)); tikhomirov@22: } tikhomirov@22: tikhomirov@2: public byte[] content() { tikhomirov@2: return content(TIP); tikhomirov@2: } tikhomirov@22: tikhomirov@3: public void history(Changeset.Inspector inspector) { tikhomirov@48: history(0, content.revisionCount() - 1, inspector); tikhomirov@48: } tikhomirov@48: tikhomirov@48: public void history(int start, int end, Changeset.Inspector inspector) { tikhomirov@3: if (!exists()) { tikhomirov@3: throw new IllegalStateException("Can't get history of invalid repository file node"); tikhomirov@3: } tikhomirov@48: final int[] commitRevisions = new int[end - start + 1]; tikhomirov@3: Revlog.Inspector insp = new Revlog.Inspector() { tikhomirov@3: int count = 0; tikhomirov@3: tikhomirov@3: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) { tikhomirov@3: commitRevisions[count++] = linkRevision; tikhomirov@3: } tikhomirov@3: }; tikhomirov@48: content.iterate(start, end, false, insp); tikhomirov@3: getRepo().getChangelog().range(inspector, commitRevisions); tikhomirov@3: } tikhomirov@3: tikhomirov@17: /** tikhomirov@17: * XXX perhaps, return value Nodeid[2] and boolean needNodeids is better (and higher level) API for this query? tikhomirov@17: * tikhomirov@17: * @param revision - revision to query parents, or {@link HgRepository#TIP} tikhomirov@17: * @param parentRevisions - int[2] to get local revision numbers of parents (e.g. {6, -1}) tikhomirov@17: * @param parent1 - byte[20] or null, if parent's nodeid is not needed tikhomirov@17: * @param parent2 - byte[20] or null, if second parent's nodeid is not needed tikhomirov@17: * @return tikhomirov@17: */ tikhomirov@17: public void parents(int revision, int[] parentRevisions, byte[] parent1, byte[] parent2) { tikhomirov@17: if (revision != TIP && !(revision >= 0 && revision < content.revisionCount())) { tikhomirov@17: throw new IllegalArgumentException(String.valueOf(revision)); tikhomirov@17: } tikhomirov@17: if (parentRevisions == null || parentRevisions.length < 2) { tikhomirov@17: throw new IllegalArgumentException(String.valueOf(parentRevisions)); tikhomirov@17: } tikhomirov@17: if (parent1 != null && parent1.length < 20) { tikhomirov@17: throw new IllegalArgumentException(parent1.toString()); tikhomirov@17: } tikhomirov@17: if (parent2 != null && parent2.length < 20) { tikhomirov@17: throw new IllegalArgumentException(parent2.toString()); tikhomirov@17: } tikhomirov@17: class ParentCollector implements Revlog.Inspector { tikhomirov@17: public int p1 = -1; tikhomirov@17: public int p2 = -1; tikhomirov@17: public byte[] nodeid; tikhomirov@17: tikhomirov@17: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) { tikhomirov@17: p1 = parent1Revision; tikhomirov@17: p2 = parent2Revision; tikhomirov@17: this.nodeid = new byte[20]; tikhomirov@17: // nodeid arg now comes in 32 byte from (as in file format description), however upper 12 bytes are zeros. tikhomirov@17: System.arraycopy(nodeid, nodeid.length > 20 ? nodeid.length - 20 : 0, this.nodeid, 0, 20); tikhomirov@17: } tikhomirov@17: }; tikhomirov@17: ParentCollector pc = new ParentCollector(); tikhomirov@17: content.iterate(revision, revision, false, pc); tikhomirov@17: parentRevisions[0] = pc.p1; tikhomirov@17: parentRevisions[1] = pc.p2; tikhomirov@17: if (parent1 != null) { tikhomirov@17: if (parentRevisions[0] == -1) { tikhomirov@17: Arrays.fill(parent1, 0, 20, (byte) 0); tikhomirov@17: } else { tikhomirov@17: content.iterate(parentRevisions[0], parentRevisions[0], false, pc); tikhomirov@17: System.arraycopy(pc.nodeid, 0, parent1, 0, 20); tikhomirov@17: } tikhomirov@17: } tikhomirov@17: if (parent2 != null) { tikhomirov@17: if (parentRevisions[1] == -1) { tikhomirov@17: Arrays.fill(parent2, 0, 20, (byte) 0); tikhomirov@17: } else { tikhomirov@17: content.iterate(parentRevisions[1], parentRevisions[1], false, pc); tikhomirov@17: System.arraycopy(pc.nodeid, 0, parent2, 0, 20); tikhomirov@17: } tikhomirov@17: } tikhomirov@17: } tikhomirov@2: }