tikhomirov@248: /* tikhomirov@396: * Copyright (c) 2011-2012 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@423: import org.tmatesoft.hg.repo.HgInvalidStateException; tikhomirov@415: import org.tmatesoft.hg.repo.HgManifest; tikhomirov@248: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@423: import org.tmatesoft.hg.repo.HgRuntimeException; tikhomirov@248: import org.tmatesoft.hg.util.Path; tikhomirov@454: import org.tmatesoft.hg.util.Outcome; 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@417:  *   HgChangesetFileSneaker i = new HgChangesetFileSneaker(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@417: * TODO may add #manifest(Nodeid) to select manifest according to its revision (not only changeset revision as it's now) tikhomirov@354: * tikhomirov@417: *

Unlike {@link HgManifest#getFileRevision(int, Path)}, this class is useful when few files from the same changeset have to be inspected tikhomirov@417: * tikhomirov@417: * @see HgManifest#getFileRevision(int, Path) tikhomirov@248: * @author Artem Tikhomirov tikhomirov@248: * @author TMate Software Ltd. tikhomirov@248: */ tikhomirov@434: public final class HgChangesetFileSneaker { 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@454: private Outcome checkResult; tikhomirov@248: tikhomirov@417: public HgChangesetFileSneaker(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@417: public HgChangesetFileSneaker 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@417: public HgChangesetFileSneaker followRenames(boolean follow) { tikhomirov@248: followRenames = follow; tikhomirov@248: fileRevision = null; tikhomirov@248: return this; tikhomirov@248: } tikhomirov@248: tikhomirov@251: /** tikhomirov@454: * Shortcut to perform {@link #check(Path)} and {@link #exists()}. Result of the check may be accessed via {@link #getCheckResult()}. tikhomirov@427: * Errors during the check, if any, are reported through exception. 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@427: * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state tikhomirov@251: * @throws IllegalArgumentException if {@link #changeset(Nodeid)} not specified or file argument is bad. tikhomirov@251: */ tikhomirov@427: public boolean checkExists(Path file) throws HgException { tikhomirov@361: check(file); tikhomirov@427: // next seems reasonable, however renders boolean return value useless. perhaps void or distinct method? tikhomirov@427: // if (checkResult.isOk() && !exists()) { tikhomirov@427: // throw new HgPathNotFoundException(checkResult.getMessage(), file); tikhomirov@427: // } tikhomirov@427: if (!checkResult.isOk() && checkResult.getException() instanceof HgRuntimeException) { tikhomirov@427: throw new HgLibraryFailureException((HgRuntimeException) 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@454: * @return status object that describes outcome, {@link Outcome#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@454: public Outcome 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@454: checkResult = new Outcome(Outcome.Kind.Success, String.format("File named %s is not known in the repository", file)); tikhomirov@361: return checkResult; tikhomirov@248: } tikhomirov@361: Nodeid toExtract = null; tikhomirov@415: HgManifest.Flags extractRevFlags = null; tikhomirov@396: String phaseMsg = "Extract manifest revision failed"; 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@415: extractRevFlags = cachedManifest.flags(file); tikhomirov@396: phaseMsg = "Follow copy/rename failed"; 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@415: extractRevFlags = cachedManifest.flags(file); tikhomirov@248: } tikhomirov@248: } tikhomirov@423: } catch (HgRuntimeException ex) { tikhomirov@454: checkResult = new Outcome(Outcome.Kind.Failure, phaseMsg, ex); tikhomirov@361: return checkResult; tikhomirov@248: } tikhomirov@248: if (toExtract != null) { tikhomirov@415: fileRevision = new HgFileRevision(repo, toExtract, extractRevFlags, dataFile.getPath()); tikhomirov@454: checkResult = new Outcome(Outcome.Kind.Success, String.format("File %s, revision %s found at changeset %s", dataFile.getPath(), toExtract.shortNotation(), cset.shortNotation())); tikhomirov@361: return checkResult; tikhomirov@361: } tikhomirov@454: checkResult = new Outcome(Outcome.Kind.Success, String.format("File %s nor its origins were 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@454: public Outcome getCheckResult() { 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@423: throw new HgInvalidStateException("Shall invoke #check(Path) first"); tikhomirov@248: } tikhomirov@248: } tikhomirov@417: tikhomirov@248: }