tikhomirov@248: /* tikhomirov@248: * Copyright (c) 2011 TMate Software Ltd tikhomirov@248: * tikhomirov@248: * This program is free software; you can redistribute it and/or modify tikhomirov@248: * it under the terms of the GNU General Public License as published by tikhomirov@248: * the Free Software Foundation; version 2 of the License. tikhomirov@248: * tikhomirov@248: * This program is distributed in the hope that it will be useful, tikhomirov@248: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@248: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@248: * GNU General Public License for more details. tikhomirov@248: * tikhomirov@248: * For information on how to redistribute this software under tikhomirov@248: * the terms of a license other than GNU General Public License tikhomirov@248: * contact TMate Software at support@hg4j.com tikhomirov@248: */ tikhomirov@248: package org.tmatesoft.hg.core; tikhomirov@248: tikhomirov@248: import org.tmatesoft.hg.internal.ManifestRevision; tikhomirov@248: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@295: import org.tmatesoft.hg.repo.HgInternals; tikhomirov@248: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@248: import org.tmatesoft.hg.util.Path; tikhomirov@361: import org.tmatesoft.hg.util.Status; tikhomirov@248: tikhomirov@248: /** tikhomirov@248: * Primary purpose is to provide information about file revisions at specific changeset. Multiple {@link #check(Path)} calls tikhomirov@248: * are possible once {@link #changeset(Nodeid)} (and optionally, {@link #followRenames(boolean)}) were set. tikhomirov@251: * tikhomirov@251: *

Sample: tikhomirov@251: *


tikhomirov@251:  *   HgFileInformer i = new HgFileInformer(hgRepo).changeset(Nodeid.fromString("<40 digits>")).followRenames(true);
tikhomirov@251:  *   if (i.check(file)) {
tikhomirov@251:  *   	HgCatCommand catCmd = new HgCatCommand(hgRepo).revision(i.getFileRevision());
tikhomirov@251:  *   	catCmd.execute(...);
tikhomirov@251:  *   	...
tikhomirov@251:  *   }
tikhomirov@251:  * 
tikhomirov@248: * tikhomirov@354: * FIXME need better name. It's more about manifest of specific changeset, rather than informing (about) files tikhomirov@361: * TODO may add #manifest(Nodeid) to select manifest according to its revision (not only changeset revision as it's now) tikhomirov@354: * tikhomirov@248: * @author Artem Tikhomirov tikhomirov@248: * @author TMate Software Ltd. tikhomirov@248: */ tikhomirov@248: public class HgFileInformer { tikhomirov@248: tikhomirov@248: private final HgRepository repo; tikhomirov@248: private boolean followRenames; tikhomirov@248: private Nodeid cset; tikhomirov@248: private ManifestRevision cachedManifest; tikhomirov@248: private HgFileRevision fileRevision; tikhomirov@361: private boolean renamed; tikhomirov@361: private Status checkResult; tikhomirov@248: tikhomirov@248: public HgFileInformer(HgRepository hgRepo) { tikhomirov@248: repo = hgRepo; tikhomirov@248: } tikhomirov@248: tikhomirov@251: /** tikhomirov@251: * Select specific changelog revision tikhomirov@251: * tikhomirov@251: * @param nid changeset identifier tikhomirov@251: * @return this for convenience tikhomirov@251: */ tikhomirov@248: public HgFileInformer changeset(Nodeid nid) { tikhomirov@274: if (nid == null || nid.isNull()) { tikhomirov@248: throw new IllegalArgumentException(); tikhomirov@248: } tikhomirov@248: cset = nid; tikhomirov@248: cachedManifest = null; tikhomirov@248: fileRevision = null; tikhomirov@248: return this; tikhomirov@248: } tikhomirov@251: tikhomirov@251: /** tikhomirov@251: * Whether to check file origins, default is false (look up only the name supplied) tikhomirov@251: * tikhomirov@251: * @param follow true to check copy/rename origin of the file if it is a copy. tikhomirov@251: * @return this for convenience tikhomirov@251: */ tikhomirov@248: public HgFileInformer followRenames(boolean follow) { tikhomirov@248: followRenames = follow; tikhomirov@248: fileRevision = null; tikhomirov@248: return this; tikhomirov@248: } tikhomirov@248: tikhomirov@251: /** tikhomirov@361: * Shortcut to perform {@link #check(Path)} and {@link #exists()}. Result of the check may be accessed via {@link #getCheckStatus()}. tikhomirov@251: * tikhomirov@251: * @param file name of the file in question tikhomirov@251: * @return true if file is known at the selected changeset. tikhomirov@251: * @throws IllegalArgumentException if {@link #changeset(Nodeid)} not specified or file argument is bad. tikhomirov@361: * @throws HgInvalidControlFileException if access to revlog index/data entry failed tikhomirov@251: */ tikhomirov@361: public boolean checkExists(Path file) throws HgInvalidControlFileException { tikhomirov@361: check(file); tikhomirov@361: if (!checkResult.isOk() && checkResult.getException() instanceof HgInvalidControlFileException) { tikhomirov@361: throw (HgInvalidControlFileException) checkResult.getException(); tikhomirov@361: } tikhomirov@361: return checkResult.isOk() && exists(); tikhomirov@361: } tikhomirov@361: tikhomirov@361: /** tikhomirov@361: * Find file (or its origin, if {@link #followRenames(boolean)} was set to true) among files known at specified {@link #changeset(Nodeid)}. tikhomirov@361: * tikhomirov@361: * @param file name of the file in question tikhomirov@361: * @return status object that describes outcome, {@link Status#isOk() Ok} status indicates successful completion of the operation, but doesn't imply tikhomirov@361: * file existence, use {@link #exists()} for that purpose. Message of the status may provide further hints on what exactly had happened. tikhomirov@361: * @throws IllegalArgumentException if {@link #changeset(Nodeid)} not specified or file argument is bad. tikhomirov@361: */ tikhomirov@361: public Status check(Path file) { tikhomirov@248: fileRevision = null; tikhomirov@361: checkResult = null; tikhomirov@248: renamed = false; tikhomirov@248: if (cset == null || file == null || file.isDirectory()) { tikhomirov@248: throw new IllegalArgumentException(); tikhomirov@248: } tikhomirov@248: HgDataFile dataFile = repo.getFileNode(file); tikhomirov@248: if (!dataFile.exists()) { tikhomirov@361: checkResult = new Status(Status.Kind.OK, String.format("File named %s is not known in the repository", file)); tikhomirov@361: return checkResult; tikhomirov@248: } tikhomirov@361: Nodeid toExtract = null; tikhomirov@248: try { tikhomirov@361: if (cachedManifest == null) { tikhomirov@367: int csetRev = repo.getChangelog().getRevisionIndex(cset); tikhomirov@361: cachedManifest = new ManifestRevision(null, null); // XXX how about context and cached manifest revisions tikhomirov@361: repo.getManifest().walk(csetRev, csetRev, cachedManifest); tikhomirov@367: // cachedManifest shall be meaningful - changelog.getRevisionIndex() above ensures we've got version that exists. tikhomirov@361: } tikhomirov@361: toExtract = cachedManifest.nodeid(file); tikhomirov@248: if (toExtract == null && followRenames) { tikhomirov@248: while (toExtract == null && dataFile.isCopy()) { tikhomirov@248: renamed = true; tikhomirov@248: file = dataFile.getCopySourceName(); tikhomirov@248: dataFile = repo.getFileNode(file); tikhomirov@285: toExtract = cachedManifest.nodeid(file); tikhomirov@248: } tikhomirov@248: } tikhomirov@361: } catch (HgInvalidControlFileException ex) { tikhomirov@361: checkResult = new Status(Status.Kind.ERROR, "", ex); tikhomirov@361: return checkResult; tikhomirov@248: } catch (HgDataStreamException ex) { tikhomirov@361: checkResult = new Status(Status.Kind.ERROR, "Follow copy/rename failed", ex); tikhomirov@361: HgInternals.getContext(repo).getLog().warn(getClass(), ex, checkResult.getMessage()); tikhomirov@361: return checkResult; tikhomirov@248: } tikhomirov@248: if (toExtract != null) { tikhomirov@248: fileRevision = new HgFileRevision(repo, toExtract, dataFile.getPath()); tikhomirov@361: checkResult = new Status(Status.Kind.OK, String.format("File %s, revision %s found at changeset %s", dataFile.getPath(), toExtract.shortNotation(), cset.shortNotation())); tikhomirov@361: return checkResult; tikhomirov@361: } tikhomirov@361: checkResult = new Status(Status.Kind.OK, String.format("File %s nor its origins were not known at repository %s revision", file, cset.shortNotation())); tikhomirov@361: return checkResult; tikhomirov@361: } tikhomirov@361: tikhomirov@361: /** tikhomirov@361: * Re-get latest check status object tikhomirov@361: */ tikhomirov@361: public Status getCheckStatus() { tikhomirov@361: assertCheckRan(); tikhomirov@361: return checkResult; tikhomirov@248: } tikhomirov@248: tikhomirov@248: /** tikhomirov@248: * @return result of the last {@link #check(Path)} call. tikhomirov@248: */ tikhomirov@248: public boolean exists() { tikhomirov@248: assertCheckRan(); tikhomirov@248: return fileRevision != null; tikhomirov@248: } tikhomirov@248: tikhomirov@248: /** tikhomirov@248: * @return true if checked file was known by another name at the time of specified changeset. tikhomirov@248: */ tikhomirov@248: public boolean hasAnotherName() { tikhomirov@248: assertCheckRan(); tikhomirov@248: return renamed; tikhomirov@248: } tikhomirov@248: tikhomirov@248: /** tikhomirov@248: * @return holder for file revision information tikhomirov@248: */ tikhomirov@248: public HgFileRevision getFileRevision() { tikhomirov@248: assertCheckRan(); tikhomirov@248: return fileRevision; tikhomirov@248: } tikhomirov@248: tikhomirov@248: /** tikhomirov@248: * Name of the checked file as it was known at the time of the specified changeset. tikhomirov@248: * tikhomirov@248: * @return handy shortcut for getFileRevision().getPath() tikhomirov@248: */ tikhomirov@248: public Path filename() { tikhomirov@248: assertCheckRan(); tikhomirov@248: return fileRevision.getPath(); tikhomirov@248: } tikhomirov@248: tikhomirov@248: /** tikhomirov@248: * Revision of the checked file tikhomirov@248: * tikhomirov@248: * @return handy shortcut for getFileRevision().getRevision() tikhomirov@248: */ tikhomirov@248: public Nodeid revision() { tikhomirov@248: assertCheckRan(); tikhomirov@248: return fileRevision.getRevision(); tikhomirov@248: } tikhomirov@248: tikhomirov@248: private void assertCheckRan() { tikhomirov@361: if (checkResult == null) { tikhomirov@248: throw new HgBadStateException("Shall invoke #check(Path) first"); tikhomirov@248: } tikhomirov@248: } tikhomirov@248: }