tikhomirov@55: /* tikhomirov@74: * Copyright (c) 2011 TMate Software Ltd tikhomirov@74: * tikhomirov@74: * This program is free software; you can redistribute it and/or modify tikhomirov@74: * it under the terms of the GNU General Public License as published by tikhomirov@74: * the Free Software Foundation; version 2 of the License. tikhomirov@74: * tikhomirov@74: * This program is distributed in the hope that it will be useful, tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@74: * GNU General Public License for more details. tikhomirov@74: * tikhomirov@74: * For information on how to redistribute this software under tikhomirov@74: * the terms of a license other than GNU General Public License tikhomirov@74: * contact TMate Software at support@svnkit.com tikhomirov@55: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@55: tikhomirov@74: import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; tikhomirov@74: import static org.tmatesoft.hg.repo.HgRepository.TIP; tikhomirov@68: tikhomirov@55: import java.util.Collection; tikhomirov@55: import java.util.Collections; tikhomirov@55: import java.util.LinkedHashMap; tikhomirov@55: import java.util.LinkedList; tikhomirov@55: import java.util.List; tikhomirov@55: import java.util.Map; tikhomirov@89: import java.util.TreeMap; tikhomirov@55: import java.util.TreeSet; tikhomirov@55: tikhomirov@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@88: import org.tmatesoft.hg.core.Path; tikhomirov@93: import org.tmatesoft.hg.util.PathPool; tikhomirov@93: import org.tmatesoft.hg.util.PathRewrite; tikhomirov@74: tikhomirov@74: tikhomirov@55: /** tikhomirov@55: * RevisionWalker? tikhomirov@74: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@55: */ tikhomirov@94: public class HgStatusCollector { tikhomirov@55: tikhomirov@55: private final HgRepository repo; tikhomirov@55: private final Map cache; // sparse array, in fact tikhomirov@93: private PathPool pathPool; tikhomirov@55: tikhomirov@94: public HgStatusCollector(HgRepository hgRepo) { tikhomirov@55: this.repo = hgRepo; tikhomirov@93: cache = new TreeMap(); tikhomirov@89: ManifestRevisionInspector emptyFakeState = new ManifestRevisionInspector(); tikhomirov@56: emptyFakeState.begin(-1, null); tikhomirov@68: emptyFakeState.end(-1); // FIXME HgRepo.TIP == -1 as well, need to distinguish fake "prior to first" revision from "the very last" tikhomirov@56: cache.put(-1, emptyFakeState); tikhomirov@55: } tikhomirov@55: tikhomirov@64: public HgRepository getRepo() { tikhomirov@64: return repo; tikhomirov@64: } tikhomirov@64: tikhomirov@55: private ManifestRevisionInspector get(int rev) { tikhomirov@55: ManifestRevisionInspector i = cache.get(rev); tikhomirov@55: if (i == null) { tikhomirov@89: i = new ManifestRevisionInspector(); tikhomirov@55: cache.put(rev, i); tikhomirov@55: repo.getManifest().walk(rev, rev, i); tikhomirov@55: } tikhomirov@55: return i; tikhomirov@55: } tikhomirov@56: tikhomirov@59: /*package-local*/ ManifestRevisionInspector raw(int rev) { tikhomirov@59: return get(rev); tikhomirov@59: } tikhomirov@93: /*package-local*/ PathPool getPathPool() { tikhomirov@93: if (pathPool == null) { tikhomirov@93: pathPool = new PathPool(new PathRewrite.Empty()); tikhomirov@93: } tikhomirov@93: return pathPool; tikhomirov@93: } tikhomirov@93: tikhomirov@93: public void setPathPool(PathPool pathPool) { tikhomirov@93: this.pathPool = pathPool; tikhomirov@93: } tikhomirov@93: tikhomirov@59: tikhomirov@56: // hg status --change tikhomirov@93: public void change(int rev, HgStatusInspector inspector) { tikhomirov@56: int[] parents = new int[2]; tikhomirov@56: repo.getChangelog().parents(rev, parents, null, null); tikhomirov@56: walk(parents[0], rev, inspector); tikhomirov@56: } tikhomirov@55: tikhomirov@56: // I assume revision numbers are the same for changelog and manifest - here tikhomirov@56: // user would like to pass changelog revision numbers, and I use them directly to walk manifest. tikhomirov@56: // if this assumption is wrong, fix this (lookup manifest revisions from changeset). tikhomirov@93: public void walk(int rev1, int rev2, HgStatusInspector inspector) { tikhomirov@55: if (rev1 == rev2) { tikhomirov@55: throw new IllegalArgumentException(); tikhomirov@55: } tikhomirov@64: if (inspector == null) { tikhomirov@64: throw new IllegalArgumentException(); tikhomirov@64: } tikhomirov@64: if (inspector instanceof Record) { tikhomirov@64: ((Record) inspector).init(rev1, rev2, this); tikhomirov@64: } tikhomirov@68: if (rev1 == TIP) { tikhomirov@68: rev1 = repo.getManifest().getRevisionCount() - 1; tikhomirov@68: } tikhomirov@68: if (rev2 == TIP) { tikhomirov@68: rev2 = repo.getManifest().getRevisionCount() - 1; // XXX add Revlog.tip() func ? tikhomirov@68: } tikhomirov@55: // in fact, rev1 and rev2 are often next (or close) to each other, tikhomirov@55: // thus, we can optimize Manifest reads here (manifest.walk(rev1, rev2)) tikhomirov@89: ManifestRevisionInspector r1, r2 ; tikhomirov@55: if (!cache.containsKey(rev1) && !cache.containsKey(rev2) && Math.abs(rev1 - rev2) < 5 /*subjective equivalent of 'close enough'*/) { tikhomirov@55: int minRev = rev1 < rev2 ? rev1 : rev2; tikhomirov@55: int maxRev = minRev == rev1 ? rev2 : rev1; tikhomirov@89: if (minRev > 0) { tikhomirov@89: minRev--; // expand range a bit tikhomirov@89: // XXX perhaps, if revlog.baseRevision is cheap, shall expand minRev up to baseRevision tikhomirov@89: // which gonna be read anyway tikhomirov@55: } tikhomirov@89: tikhomirov@89: repo.getManifest().walk(minRev, maxRev, new HgManifest.Inspector() { tikhomirov@89: private ManifestRevisionInspector delegate; tikhomirov@89: tikhomirov@89: public boolean begin(int revision, Nodeid nid) { tikhomirov@89: cache.put(revision, delegate = new ManifestRevisionInspector()); tikhomirov@89: delegate.begin(revision, nid); tikhomirov@89: return true; tikhomirov@89: } tikhomirov@89: tikhomirov@89: public boolean next(Nodeid nid, String fname, String flags) { tikhomirov@89: delegate.next(nid, fname, flags); tikhomirov@89: return true; tikhomirov@89: } tikhomirov@89: tikhomirov@89: public boolean end(int revision) { tikhomirov@89: delegate.end(revision); tikhomirov@89: delegate = null; tikhomirov@89: return true; tikhomirov@89: } tikhomirov@89: }); tikhomirov@55: } tikhomirov@89: r1 = get(rev1); tikhomirov@89: r2 = get(rev2); tikhomirov@89: tikhomirov@93: PathPool pp = getPathPool(); tikhomirov@93: tikhomirov@89: TreeSet r1Files = new TreeSet(r1.files()); tikhomirov@89: for (String fname : r2.files()) { tikhomirov@55: if (r1Files.remove(fname)) { tikhomirov@89: Nodeid nidR1 = r1.nodeid(fname); tikhomirov@89: Nodeid nidR2 = r2.nodeid(fname); tikhomirov@89: String flagsR1 = r1.flags(fname); tikhomirov@89: String flagsR2 = r2.flags(fname); tikhomirov@55: if (nidR1.equals(nidR2) && ((flagsR2 == null && flagsR1 == null) || flagsR2.equals(flagsR1))) { tikhomirov@93: inspector.clean(pp.path(fname)); tikhomirov@55: } else { tikhomirov@93: inspector.modified(pp.path(fname)); tikhomirov@55: } tikhomirov@55: } else { tikhomirov@90: String copyOrigin = getOriginIfCopy(repo, fname, r1Files, rev1); tikhomirov@90: if (copyOrigin != null) { tikhomirov@93: inspector.copied(pp.path(copyOrigin), pp.path(fname)); tikhomirov@90: } else { tikhomirov@93: inspector.added(pp.path(fname)); tikhomirov@88: } tikhomirov@55: } tikhomirov@55: } tikhomirov@55: for (String left : r1Files) { tikhomirov@93: inspector.removed(pp.path(left)); tikhomirov@55: } tikhomirov@55: } tikhomirov@55: tikhomirov@55: public Record status(int rev1, int rev2) { tikhomirov@55: Record rv = new Record(); tikhomirov@55: walk(rev1, rev2, rv); tikhomirov@55: return rv; tikhomirov@55: } tikhomirov@90: tikhomirov@90: /*package-local*/static String getOriginIfCopy(HgRepository hgRepo, String fname, Collection originals, int originalChangelogRevision) { tikhomirov@90: HgDataFile df = hgRepo.getFileNode(fname); tikhomirov@90: while (df.isCopy()) { tikhomirov@90: Path original = df.getCopySourceName(); tikhomirov@90: if (originals.contains(original.toString())) { tikhomirov@90: df = hgRepo.getFileNode(original); tikhomirov@90: int changelogRevision = df.getChangesetLocalRevision(0); tikhomirov@90: if (changelogRevision <= originalChangelogRevision) { tikhomirov@90: // copy/rename source was known prior to rev1 tikhomirov@90: // (both r1Files.contains is true and original was created earlier than rev1) tikhomirov@90: // without r1Files.contains changelogRevision <= rev1 won't suffice as the file tikhomirov@90: // might get removed somewhere in between (changelogRevision < R < rev1) tikhomirov@90: return original.toString(); tikhomirov@90: } tikhomirov@90: break; // copy/rename done later tikhomirov@90: } tikhomirov@90: df = hgRepo.getFileNode(original); // try more steps away tikhomirov@90: } tikhomirov@90: return null; tikhomirov@90: } tikhomirov@55: tikhomirov@55: // XXX for r1..r2 status, only modified, added, removed (and perhaps, clean) make sense tikhomirov@74: // XXX Need to specify whether copy targets are in added or not (@see Inspector#copied above) tikhomirov@93: public static class Record implements HgStatusInspector { tikhomirov@93: private List modified, added, removed, clean, missing, unknown, ignored; tikhomirov@93: private Map copied; tikhomirov@55: tikhomirov@64: private int startRev, endRev; tikhomirov@94: private HgStatusCollector statusHelper; tikhomirov@64: tikhomirov@64: // XXX StatusCollector may additionally initialize Record instance to speed lookup of changed file revisions tikhomirov@64: // here I need access to ManifestRevisionInspector via #raw(). Perhaps, non-static class (to get tikhomirov@64: // implicit reference to StatusCollector) may be better? tikhomirov@64: // Since users may want to reuse Record instance we've once created (and initialized), we need to tikhomirov@64: // ensure functionality is correct for each/any call (#walk checks instanceof Record and fixes it up) tikhomirov@64: // Perhaps, distinct helper (sc.getRevisionHelper().nodeid(fname)) would be better, just not clear tikhomirov@64: // how to supply [start..end] values there easily tikhomirov@94: /*package-local*/void init(int startRevision, int endRevision, HgStatusCollector self) { tikhomirov@64: startRev = startRevision; tikhomirov@64: endRev = endRevision; tikhomirov@64: statusHelper = self; tikhomirov@64: } tikhomirov@64: tikhomirov@93: public Nodeid nodeidBeforeChange(Path fname) { tikhomirov@68: if (statusHelper == null || startRev == BAD_REVISION) { tikhomirov@68: return null; tikhomirov@68: } tikhomirov@64: if ((modified == null || !modified.contains(fname)) && (removed == null || !removed.contains(fname))) { tikhomirov@64: return null; tikhomirov@64: } tikhomirov@93: return statusHelper.raw(startRev).nodeid(fname.toString()); tikhomirov@64: } tikhomirov@93: public Nodeid nodeidAfterChange(Path fname) { tikhomirov@68: if (statusHelper == null || endRev == BAD_REVISION) { tikhomirov@68: return null; tikhomirov@68: } tikhomirov@64: if ((modified == null || !modified.contains(fname)) && (added == null || !added.contains(fname))) { tikhomirov@64: return null; tikhomirov@64: } tikhomirov@93: return statusHelper.raw(endRev).nodeid(fname.toString()); tikhomirov@64: } tikhomirov@64: tikhomirov@93: public List getModified() { tikhomirov@55: return proper(modified); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public List getAdded() { tikhomirov@55: return proper(added); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public List getRemoved() { tikhomirov@55: return proper(removed); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public Map getCopied() { tikhomirov@55: if (copied == null) { tikhomirov@55: return Collections.emptyMap(); tikhomirov@55: } tikhomirov@55: return Collections.unmodifiableMap(copied); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public List getClean() { tikhomirov@55: return proper(clean); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public List getMissing() { tikhomirov@55: return proper(missing); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public List getUnknown() { tikhomirov@55: return proper(unknown); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public List getIgnored() { tikhomirov@55: return proper(ignored); tikhomirov@55: } tikhomirov@55: tikhomirov@93: private List proper(List l) { tikhomirov@55: if (l == null) { tikhomirov@55: return Collections.emptyList(); tikhomirov@55: } tikhomirov@55: return Collections.unmodifiableList(l); tikhomirov@55: } tikhomirov@55: tikhomirov@55: // tikhomirov@55: // tikhomirov@55: tikhomirov@93: public void modified(Path fname) { tikhomirov@55: modified = doAdd(modified, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public void added(Path fname) { tikhomirov@55: added = doAdd(added, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public void copied(Path fnameOrigin, Path fnameAdded) { tikhomirov@55: if (copied == null) { tikhomirov@93: copied = new LinkedHashMap(); tikhomirov@55: } tikhomirov@74: added(fnameAdded); tikhomirov@74: copied.put(fnameAdded, fnameOrigin); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public void removed(Path fname) { tikhomirov@55: removed = doAdd(removed, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public void clean(Path fname) { tikhomirov@55: clean = doAdd(clean, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public void missing(Path fname) { tikhomirov@55: missing = doAdd(missing, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public void unknown(Path fname) { tikhomirov@55: unknown = doAdd(unknown, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@93: public void ignored(Path fname) { tikhomirov@55: ignored = doAdd(ignored, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@93: private static List doAdd(List l, Path p) { tikhomirov@55: if (l == null) { tikhomirov@93: l = new LinkedList(); tikhomirov@55: } tikhomirov@93: l.add(p); tikhomirov@55: return l; tikhomirov@55: } tikhomirov@55: } tikhomirov@55: tikhomirov@89: /*package-local*/ static final class ManifestRevisionInspector implements HgManifest.Inspector { tikhomirov@89: private final TreeMap idsMap; tikhomirov@89: private final TreeMap flagsMap; tikhomirov@55: tikhomirov@89: public ManifestRevisionInspector() { tikhomirov@89: idsMap = new TreeMap(); tikhomirov@89: flagsMap = new TreeMap(); tikhomirov@55: } tikhomirov@55: tikhomirov@89: public Collection files() { tikhomirov@89: return idsMap.keySet(); tikhomirov@55: } tikhomirov@55: tikhomirov@89: public Nodeid nodeid(String fname) { tikhomirov@89: return idsMap.get(fname); tikhomirov@55: } tikhomirov@55: tikhomirov@89: public String flags(String fname) { tikhomirov@89: return flagsMap.get(fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: // tikhomirov@55: tikhomirov@55: public boolean next(Nodeid nid, String fname, String flags) { tikhomirov@89: idsMap.put(fname, nid); tikhomirov@89: flagsMap.put(fname, flags); tikhomirov@55: return true; tikhomirov@55: } tikhomirov@55: tikhomirov@55: public boolean end(int revision) { tikhomirov@89: // in fact, this class cares about single revision tikhomirov@89: return false; tikhomirov@55: } tikhomirov@55: tikhomirov@55: public boolean begin(int revision, Nodeid nid) { tikhomirov@55: return true; tikhomirov@55: } tikhomirov@55: } tikhomirov@55: tikhomirov@55: }