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@248: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@248: import org.tmatesoft.hg.util.Path; 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@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@248: private boolean checked, renamed;
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@248: if (nid == null || Nodeid.NULL.equals(nid)) {
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@251: * Find file (or its origin, if {@link #followRenames(boolean)} was set to true
) among files known at specified {@link #changeset(Nodeid)}.
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@251: */
tikhomirov@248: public boolean check(Path file) { // XXX IStatus instead of boolean?
tikhomirov@248: fileRevision = null;
tikhomirov@248: checked = false;
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@248: return false;
tikhomirov@248: }
tikhomirov@248: if (cachedManifest == null) {
tikhomirov@248: int csetRev = repo.getChangelog().getLocalRevision(cset);
tikhomirov@248: cachedManifest = new ManifestRevision(null, null); // XXX how about context and cached manifest revisions
tikhomirov@248: repo.getManifest().walk(csetRev, csetRev, cachedManifest);
tikhomirov@248: // cachedManifest shall be meaningful - changelog.getLocalRevision above ensures we've got version that exists.
tikhomirov@248: }
tikhomirov@248: Nodeid toExtract = cachedManifest.nodeid(file.toString());
tikhomirov@248: try {
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@248: toExtract = cachedManifest.nodeid(file.toString());
tikhomirov@248: }
tikhomirov@248: }
tikhomirov@248: } catch (HgDataStreamException ex) {
tikhomirov@248: ex.printStackTrace(); // XXX log(INFO)
tikhomirov@248: // ignore now, however if there's IStatus retval, might report error with reasonable explanation.
tikhomirov@248: // Perhaps, may add a String reason() method with such info?
tikhomirov@248: }
tikhomirov@248: checked = true;
tikhomirov@248: if (toExtract != null) {
tikhomirov@248: fileRevision = new HgFileRevision(repo, toExtract, dataFile.getPath());
tikhomirov@248: return true;
tikhomirov@248: } // else String.format("File %s nor its origins were not known at repository %s revision", file, cset.shortNotation())
tikhomirov@248: return false;
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@248: if (!checked) {
tikhomirov@248: throw new HgBadStateException("Shall invoke #check(Path) first");
tikhomirov@248: }
tikhomirov@248: }
tikhomirov@248: }