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