tikhomirov@128: /* tikhomirov@128: * Copyright (c) 2011 TMate Software Ltd tikhomirov@128: * tikhomirov@128: * This program is free software; you can redistribute it and/or modify tikhomirov@128: * it under the terms of the GNU General Public License as published by tikhomirov@128: * the Free Software Foundation; version 2 of the License. tikhomirov@128: * tikhomirov@128: * This program is distributed in the hope that it will be useful, tikhomirov@128: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@128: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@128: * GNU General Public License for more details. tikhomirov@128: * tikhomirov@128: * For information on how to redistribute this software under tikhomirov@128: * the terms of a license other than GNU General Public License tikhomirov@128: * contact TMate Software at support@hg4j.com tikhomirov@128: */ tikhomirov@128: package org.tmatesoft.hg.internal; tikhomirov@128: tikhomirov@128: import java.util.TreeMap; tikhomirov@128: tikhomirov@128: import org.tmatesoft.hg.core.Path; tikhomirov@128: import org.tmatesoft.hg.repo.Changeset; tikhomirov@128: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@128: import org.tmatesoft.hg.repo.HgInternals; tikhomirov@128: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@128: tikhomirov@128: /** tikhomirov@128: * tikhomirov@128: * @author Artem Tikhomirov tikhomirov@128: * @author TMate Software Ltd. tikhomirov@128: */ tikhomirov@128: public class ChangelogHelper { tikhomirov@128: private final int leftBoundary; tikhomirov@128: private final HgRepository repo; tikhomirov@128: private final TreeMap cache = new TreeMap(); tikhomirov@128: tikhomirov@128: /** tikhomirov@128: * @param hgRepo tikhomirov@128: * @param leftBoundaryRevision walker never visits revisions with local numbers less than specified, tikhomirov@128: * IOW only revisions [leftBoundaryRevision..TIP] are considered. tikhomirov@128: */ tikhomirov@128: public ChangelogHelper(HgRepository hgRepo, int leftBoundaryRevision) { tikhomirov@128: repo = hgRepo; tikhomirov@128: leftBoundary = leftBoundaryRevision; tikhomirov@128: } tikhomirov@128: tikhomirov@128: /** tikhomirov@128: * @return the repo tikhomirov@128: */ tikhomirov@128: public HgRepository getRepo() { tikhomirov@128: return repo; tikhomirov@128: } tikhomirov@128: tikhomirov@128: /** tikhomirov@128: * Walks changelog in reverse order tikhomirov@128: * @param file tikhomirov@128: * @return changeset where specified file is mentioned among affected files, or tikhomirov@128: * null if none found up to leftBoundary tikhomirov@128: */ tikhomirov@128: public Changeset findLatestChangeWith(Path file) { tikhomirov@128: HgDataFile df = repo.getFileNode(file); tikhomirov@128: int changelogRev = df.getChangesetLocalRevision(HgRepository.TIP); tikhomirov@128: if (changelogRev >= leftBoundary) { tikhomirov@128: // the method is likely to be invoked for different files, tikhomirov@128: // while changesets might be the same. Cache 'em not to read too much. tikhomirov@128: Changeset cs = cache.get(changelogRev); tikhomirov@128: if (cs == null) { tikhomirov@128: cs = repo.getChangelog().range(changelogRev, changelogRev).get(0); tikhomirov@128: cache.put(changelogRev, cs); tikhomirov@128: } tikhomirov@128: return cs; tikhomirov@128: } tikhomirov@128: return null; tikhomirov@128: } tikhomirov@128: tikhomirov@128: public String getNextCommitUsername() { tikhomirov@128: return new HgInternals(repo).getNextCommitUsername(); tikhomirov@128: } tikhomirov@128: }