tikhomirov@58: /* tikhomirov@58: * Copyright (c) 2011 Artem Tikhomirov tikhomirov@58: */ tikhomirov@58: package com.tmate.hgkit.ll; tikhomirov@58: tikhomirov@58: import static com.tmate.hgkit.ll.HgRepository.TIP; tikhomirov@58: tikhomirov@58: import java.io.BufferedInputStream; tikhomirov@58: import java.io.File; tikhomirov@58: import java.io.FileInputStream; tikhomirov@58: import java.io.IOException; tikhomirov@58: import java.util.Collections; tikhomirov@58: import java.util.Set; tikhomirov@58: import java.util.TreeSet; tikhomirov@58: tikhomirov@58: import com.tmate.hgkit.fs.FileWalker; tikhomirov@58: tikhomirov@58: /** tikhomirov@58: * tikhomirov@58: * @author artem tikhomirov@58: */ tikhomirov@58: public class WorkingCopyStatusCollector { tikhomirov@58: tikhomirov@58: private final HgRepository repo; tikhomirov@58: private final FileWalker repoWalker; tikhomirov@59: private HgDirstate dirstate; tikhomirov@59: private StatusCollector baseRevisionCollector; tikhomirov@58: tikhomirov@58: public WorkingCopyStatusCollector(HgRepository hgRepo, FileWalker hgRepoWalker) { tikhomirov@58: this.repo = hgRepo; tikhomirov@58: this.repoWalker = hgRepoWalker; tikhomirov@58: } tikhomirov@59: tikhomirov@59: /** tikhomirov@59: * Optionally, supply a collector instance that may cache (or have already cached) base revision tikhomirov@59: * @param sc may be null tikhomirov@59: */ tikhomirov@63: public void setBaseRevisionCollector(StatusCollector sc) { tikhomirov@59: baseRevisionCollector = sc; tikhomirov@59: } tikhomirov@59: tikhomirov@59: private HgDirstate getDirstate() { tikhomirov@59: if (dirstate == null) { tikhomirov@59: if (repo instanceof LocalHgRepo) { tikhomirov@59: dirstate = ((LocalHgRepo) repo).loadDirstate(); tikhomirov@59: } else { tikhomirov@59: dirstate = new HgDirstate(); tikhomirov@59: } tikhomirov@59: } tikhomirov@59: return dirstate; tikhomirov@59: } tikhomirov@58: tikhomirov@59: // may be invoked few times tikhomirov@58: public void walk(int baseRevision, StatusCollector.Inspector inspector) { tikhomirov@58: final HgIgnore hgIgnore = ((LocalHgRepo) repo).loadIgnore(); // FIXME hack tikhomirov@59: TreeSet knownEntries = getDirstate().all(); tikhomirov@58: final boolean isTipBase = baseRevision == TIP || baseRevision == repo.getManifest().getRevisionCount(); tikhomirov@58: StatusCollector.ManifestRevisionInspector collect = null; tikhomirov@58: Set baseRevFiles = Collections.emptySet(); tikhomirov@58: if (!isTipBase) { tikhomirov@59: if (baseRevisionCollector != null) { tikhomirov@59: collect = baseRevisionCollector.raw(baseRevision); tikhomirov@59: } else { tikhomirov@59: collect = new StatusCollector.ManifestRevisionInspector(baseRevision, baseRevision); tikhomirov@59: repo.getManifest().walk(baseRevision, baseRevision, collect); tikhomirov@59: } tikhomirov@58: baseRevFiles = new TreeSet(collect.files(baseRevision)); tikhomirov@58: } tikhomirov@58: repoWalker.reset(); tikhomirov@58: while (repoWalker.hasNext()) { tikhomirov@58: repoWalker.next(); tikhomirov@58: String fname = repoWalker.name(); tikhomirov@58: File f = repoWalker.file(); tikhomirov@58: if (hgIgnore.isIgnored(fname)) { tikhomirov@58: inspector.ignored(fname); tikhomirov@58: } else if (knownEntries.remove(fname)) { tikhomirov@58: // modified, added, removed, clean tikhomirov@58: if (collect != null) { // need to check against base revision, not FS file tikhomirov@58: Nodeid nid1 = collect.nodeid(baseRevision, fname); tikhomirov@58: String flags = collect.flags(baseRevision, fname); tikhomirov@59: checkLocalStatusAgainstBaseRevision(baseRevFiles, nid1, flags, fname, f, inspector); tikhomirov@58: baseRevFiles.remove(fname); tikhomirov@58: } else { tikhomirov@59: checkLocalStatusAgainstFile(fname, f, inspector); tikhomirov@58: } tikhomirov@58: } else { tikhomirov@58: inspector.unknown(fname); tikhomirov@58: } tikhomirov@58: } tikhomirov@58: if (collect != null) { tikhomirov@58: for (String r : baseRevFiles) { tikhomirov@58: inspector.removed(r); tikhomirov@58: } tikhomirov@58: } tikhomirov@58: for (String m : knownEntries) { tikhomirov@58: // removed from the repository and missing from working dir shall not be reported as 'deleted' tikhomirov@59: if (getDirstate().checkRemoved(m) == null) { tikhomirov@58: inspector.missing(m); tikhomirov@58: } tikhomirov@58: } tikhomirov@58: } tikhomirov@58: tikhomirov@58: public StatusCollector.Record status(int baseRevision) { tikhomirov@58: StatusCollector.Record rv = new StatusCollector.Record(); tikhomirov@58: walk(baseRevision, rv); tikhomirov@58: return rv; tikhomirov@58: } tikhomirov@58: tikhomirov@58: //******************************************** tikhomirov@58: tikhomirov@58: tikhomirov@59: private void checkLocalStatusAgainstFile(String fname, File f, StatusCollector.Inspector inspector) { tikhomirov@58: HgDirstate.Record r; tikhomirov@59: if ((r = getDirstate().checkNormal(fname)) != null) { tikhomirov@58: // either clean or modified tikhomirov@58: if (f.lastModified() / 1000 == r.time && r.size == f.length()) { tikhomirov@58: inspector.clean(fname); tikhomirov@58: } else { tikhomirov@58: // FIXME check actual content to avoid false modified files tikhomirov@58: inspector.modified(fname); tikhomirov@58: } tikhomirov@59: } else if ((r = getDirstate().checkAdded(fname)) != null) { tikhomirov@58: if (r.name2 == null) { tikhomirov@58: inspector.added(fname); tikhomirov@58: } else { tikhomirov@58: inspector.copied(fname, r.name2); tikhomirov@58: } tikhomirov@59: } else if ((r = getDirstate().checkRemoved(fname)) != null) { tikhomirov@58: inspector.removed(fname); tikhomirov@59: } else if ((r = getDirstate().checkMerged(fname)) != null) { tikhomirov@58: inspector.modified(fname); tikhomirov@58: } tikhomirov@58: } tikhomirov@58: tikhomirov@58: // XXX refactor checkLocalStatus methods in more OO way tikhomirov@59: private void checkLocalStatusAgainstBaseRevision(Set baseRevNames, Nodeid nid1, String flags, String fname, File f, StatusCollector.Inspector inspector) { tikhomirov@58: // fname is in the dirstate, either Normal, Added, Removed or Merged tikhomirov@58: HgDirstate.Record r; tikhomirov@58: if (nid1 == null) { tikhomirov@58: // normal: added? tikhomirov@58: // added: not known at the time of baseRevision, shall report tikhomirov@58: // merged: was not known, report as added? tikhomirov@59: if ((r = getDirstate().checkAdded(fname)) != null) { tikhomirov@58: if (r.name2 != null && baseRevNames.contains(r.name2)) { tikhomirov@58: baseRevNames.remove(r.name2); tikhomirov@58: inspector.copied(r.name2, fname); tikhomirov@58: return; tikhomirov@58: } tikhomirov@58: // fall-through, report as added tikhomirov@59: } else if (getDirstate().checkRemoved(fname) != null) { tikhomirov@58: // removed: removed file was not known at the time of baseRevision, and we should not report it as removed tikhomirov@58: return; tikhomirov@58: } tikhomirov@58: inspector.added(fname); tikhomirov@58: } else { tikhomirov@58: // was known; check whether clean or modified tikhomirov@58: // when added - seems to be the case of a file added once again, hence need to check if content is different tikhomirov@59: if ((r = getDirstate().checkNormal(fname)) != null || (r = getDirstate().checkMerged(fname)) != null || (r = getDirstate().checkAdded(fname)) != null) { tikhomirov@58: // either clean or modified tikhomirov@58: HgDataFile fileNode = repo.getFileNode(fname); tikhomirov@58: final int lengthAtRevision = fileNode.length(nid1); tikhomirov@58: if (r.size /* XXX File.length() ?! */ != lengthAtRevision || flags != todoGenerateFlags(fname /*java.io.File*/)) { tikhomirov@58: inspector.modified(fname); tikhomirov@58: } else { tikhomirov@58: // check actual content to see actual changes tikhomirov@58: // XXX consider adding HgDataDile.compare(File/byte[]/whatever) operation to optimize comparison tikhomirov@58: if (areTheSame(f, fileNode.content(nid1))) { tikhomirov@58: inspector.clean(fname); tikhomirov@58: } else { tikhomirov@58: inspector.modified(fname); tikhomirov@58: } tikhomirov@58: } tikhomirov@58: } tikhomirov@58: // only those left in idsMap after processing are reported as removed tikhomirov@58: } tikhomirov@58: tikhomirov@58: // TODO think over if content comparison may be done more effectively by e.g. calculating nodeid for a local file and comparing it with nodeid from manifest tikhomirov@58: // we don't need to tell exact difference, hash should be enough to detect difference, and it doesn't involve reading historical file content, and it's relatively tikhomirov@58: // cheap to calc hash on a file (no need to keep it completely in memory). OTOH, if I'm right that the next approach is used for nodeids: tikhomirov@58: // changeset nodeid + hash(actual content) => entry (Nodeid) in the next Manifest tikhomirov@58: // then it's sufficient to check parents from dirstate, and if they do not match parents from file's baseRevision (non matching parents means different nodeids). tikhomirov@58: // The question is whether original Hg treats this case (same content, different parents and hence nodeids) as 'modified' or 'clean' tikhomirov@58: } tikhomirov@58: tikhomirov@58: private static String todoGenerateFlags(String fname) { tikhomirov@58: // FIXME implement tikhomirov@58: return null; tikhomirov@58: } tikhomirov@58: private static boolean areTheSame(File f, byte[] data) { tikhomirov@58: try { tikhomirov@58: BufferedInputStream is = new BufferedInputStream(new FileInputStream(f)); tikhomirov@58: int i = 0; tikhomirov@58: while (i < data.length && data[i] == is.read()) { tikhomirov@58: i++; // increment only for successful match, otherwise won't tell last byte in data was the same as read from the stream tikhomirov@58: } tikhomirov@58: return i == data.length && is.read() == -1; // although data length is expected to be the same (see caller), check that we reached EOF, no more data left. tikhomirov@58: } catch (IOException ex) { tikhomirov@58: ex.printStackTrace(); // log warn tikhomirov@58: } tikhomirov@58: return false; tikhomirov@58: } tikhomirov@58: tikhomirov@58: }