tikhomirov@55: /* tikhomirov@55: * Copyright (c) 2011 Artem Tikhomirov tikhomirov@55: */ tikhomirov@55: package com.tmate.hgkit.ll; tikhomirov@55: tikhomirov@55: import java.util.Collection; tikhomirov@55: import java.util.Collections; tikhomirov@55: import java.util.HashMap; 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@55: import java.util.TreeSet; tikhomirov@55: tikhomirov@55: /** tikhomirov@55: * RevisionWalker? tikhomirov@55: * @author artem tikhomirov@55: */ tikhomirov@55: public class StatusCollector { tikhomirov@55: tikhomirov@55: private final HgRepository repo; tikhomirov@55: private final Map cache; // sparse array, in fact tikhomirov@55: tikhomirov@55: public StatusCollector(HgRepository hgRepo) { tikhomirov@55: this.repo = hgRepo; tikhomirov@55: cache = new HashMap(); tikhomirov@56: ManifestRevisionInspector emptyFakeState = new ManifestRevisionInspector(-1, -1); tikhomirov@56: emptyFakeState.begin(-1, null); tikhomirov@56: emptyFakeState.end(-1); tikhomirov@56: cache.put(-1, emptyFakeState); tikhomirov@55: } tikhomirov@55: tikhomirov@55: private ManifestRevisionInspector get(int rev) { tikhomirov@55: ManifestRevisionInspector i = cache.get(rev); tikhomirov@55: if (i == null) { tikhomirov@55: i = new ManifestRevisionInspector(rev, rev); 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@59: tikhomirov@56: // hg status --change tikhomirov@56: public void change(int rev, Inspector 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@55: public void walk(int rev1, int rev2, Inspector inspector) { tikhomirov@55: if (rev1 == rev2) { tikhomirov@55: throw new IllegalArgumentException(); tikhomirov@55: } 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@55: tikhomirov@55: 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@55: r1 = r2 = new ManifestRevisionInspector(minRev, maxRev); tikhomirov@55: for (int i = minRev; i <= maxRev; i++) { tikhomirov@55: cache.put(i, r1); tikhomirov@55: } tikhomirov@55: repo.getManifest().walk(minRev, maxRev, r1); tikhomirov@55: } else { tikhomirov@55: r1 = get(rev1); tikhomirov@55: r2 = get(rev2); tikhomirov@55: } tikhomirov@55: tikhomirov@55: TreeSet r1Files = new TreeSet(r1.files(rev1)); tikhomirov@55: for (String fname : r2.files(rev2)) { tikhomirov@55: if (r1Files.remove(fname)) { tikhomirov@55: Nodeid nidR1 = r1.nodeid(rev1, fname); tikhomirov@55: Nodeid nidR2 = r2.nodeid(rev2, fname); tikhomirov@55: String flagsR1 = r1.flags(rev1, fname); tikhomirov@55: String flagsR2 = r2.flags(rev2, fname); tikhomirov@55: if (nidR1.equals(nidR2) && ((flagsR2 == null && flagsR1 == null) || flagsR2.equals(flagsR1))) { tikhomirov@55: inspector.clean(fname); tikhomirov@55: } else { tikhomirov@55: inspector.modified(fname); tikhomirov@55: } tikhomirov@55: } else { tikhomirov@55: inspector.added(fname); tikhomirov@55: } tikhomirov@55: } tikhomirov@55: for (String left : r1Files) { tikhomirov@55: inspector.removed(left); tikhomirov@55: } tikhomirov@55: // inspector.done() if useful e.g. in UI client 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@55: tikhomirov@55: public interface Inspector { tikhomirov@55: void modified(String fname); tikhomirov@55: void added(String fname); tikhomirov@55: void copied(String fnameOrigin, String fnameAdded); // if copied files of no interest, should delegate to self.added(fnameAdded); tikhomirov@55: void removed(String fname); tikhomirov@55: void clean(String fname); tikhomirov@55: void missing(String fname); // aka deleted (tracked by Hg, but not available in FS any more tikhomirov@55: void unknown(String fname); // not tracked tikhomirov@55: void ignored(String fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: // XXX for r1..r2 status, only modified, added, removed (and perhaps, clean) make sense tikhomirov@55: public static class Record implements Inspector { tikhomirov@55: private List modified, added, removed, clean, missing, unknown, ignored; tikhomirov@55: private Map copied; tikhomirov@55: tikhomirov@55: public List getModified() { tikhomirov@55: return proper(modified); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public List getAdded() { tikhomirov@55: return proper(added); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public List getRemoved() { tikhomirov@55: return proper(removed); tikhomirov@55: } tikhomirov@55: tikhomirov@55: 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@55: public List getClean() { tikhomirov@55: return proper(clean); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public List getMissing() { tikhomirov@55: return proper(missing); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public List getUnknown() { tikhomirov@55: return proper(unknown); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public List getIgnored() { tikhomirov@55: return proper(ignored); tikhomirov@55: } tikhomirov@55: tikhomirov@55: 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@55: public void modified(String fname) { tikhomirov@55: modified = doAdd(modified, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public void added(String fname) { tikhomirov@55: added = doAdd(added, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public void copied(String fnameOrigin, String fnameAdded) { tikhomirov@55: if (copied == null) { tikhomirov@55: copied = new LinkedHashMap(); tikhomirov@55: } tikhomirov@55: copied.put(fnameOrigin, fnameAdded); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public void removed(String fname) { tikhomirov@55: removed = doAdd(removed, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public void clean(String fname) { tikhomirov@55: clean = doAdd(clean, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public void missing(String fname) { tikhomirov@55: missing = doAdd(missing, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public void unknown(String fname) { tikhomirov@55: unknown = doAdd(unknown, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public void ignored(String fname) { tikhomirov@55: ignored = doAdd(ignored, fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: private static List doAdd(List l, String s) { tikhomirov@55: if (l == null) { tikhomirov@55: l = new LinkedList(); tikhomirov@55: } tikhomirov@55: l.add(s); tikhomirov@55: return l; tikhomirov@55: } tikhomirov@55: } tikhomirov@55: tikhomirov@55: public /*XXX private, actually. Made public unless repo.statusLocal finds better place*/ static final class ManifestRevisionInspector implements HgManifest.Inspector { tikhomirov@55: private final HashMap[] idsMap; tikhomirov@55: private final HashMap[] flagsMap; tikhomirov@55: private final int baseRevision; tikhomirov@55: private int r = -1; // cursor tikhomirov@55: tikhomirov@55: /** tikhomirov@55: * [minRev, maxRev] tikhomirov@56: * [-1,-1] also accepted (for fake empty instance) tikhomirov@55: * @param minRev - inclusive tikhomirov@55: * @param maxRev - inclusive tikhomirov@55: */ tikhomirov@55: @SuppressWarnings("unchecked") tikhomirov@55: public ManifestRevisionInspector(int minRev, int maxRev) { tikhomirov@55: baseRevision = minRev; tikhomirov@55: int range = maxRev - minRev + 1; tikhomirov@55: idsMap = new HashMap[range]; tikhomirov@55: flagsMap = new HashMap[range]; tikhomirov@55: } tikhomirov@55: tikhomirov@55: public Collection files(int rev) { tikhomirov@55: if (rev < baseRevision || rev >= baseRevision + idsMap.length) { tikhomirov@55: throw new IllegalArgumentException(); tikhomirov@55: } tikhomirov@55: return idsMap[rev - baseRevision].keySet(); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public Nodeid nodeid(int rev, String fname) { tikhomirov@55: if (rev < baseRevision || rev >= baseRevision + idsMap.length) { tikhomirov@55: throw new IllegalArgumentException(); tikhomirov@55: } tikhomirov@55: return idsMap[rev - baseRevision].get(fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: public String flags(int rev, String fname) { tikhomirov@55: if (rev < baseRevision || rev >= baseRevision + idsMap.length) { tikhomirov@55: throw new IllegalArgumentException(); tikhomirov@55: } tikhomirov@55: return flagsMap[rev - baseRevision].get(fname); tikhomirov@55: } tikhomirov@55: tikhomirov@55: // tikhomirov@55: tikhomirov@55: public boolean next(Nodeid nid, String fname, String flags) { tikhomirov@55: idsMap[r].put(fname, nid); tikhomirov@55: flagsMap[r].put(fname, flags); tikhomirov@55: return true; tikhomirov@55: } tikhomirov@55: tikhomirov@55: public boolean end(int revision) { tikhomirov@55: assert revision == r + baseRevision; tikhomirov@55: r = -1; tikhomirov@55: return revision+1 < baseRevision + idsMap.length; tikhomirov@55: } tikhomirov@55: tikhomirov@55: public boolean begin(int revision, Nodeid nid) { tikhomirov@55: if (revision < baseRevision || revision >= baseRevision + idsMap.length) { tikhomirov@55: throw new IllegalArgumentException(); tikhomirov@55: } tikhomirov@55: r = revision - baseRevision; tikhomirov@55: idsMap[r] = new HashMap(); tikhomirov@55: flagsMap[r] = new HashMap(); tikhomirov@55: return true; tikhomirov@55: } tikhomirov@55: } tikhomirov@55: tikhomirov@55: }