tikhomirov@58: /* 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@58: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@58: tikhomirov@74: import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; tikhomirov@74: import static org.tmatesoft.hg.repo.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@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@89: import org.tmatesoft.hg.repo.StatusCollector.ManifestRevisionInspector; tikhomirov@74: import org.tmatesoft.hg.util.FileWalker; tikhomirov@58: tikhomirov@58: /** tikhomirov@58: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. 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@74: public WorkingCopyStatusCollector(HgRepository hgRepo) { tikhomirov@74: this(hgRepo, hgRepo.createWorkingDirWalker()); tikhomirov@74: } tikhomirov@74: tikhomirov@74: 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@74: dirstate = repo.loadDirstate(); 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@91: final HgIgnore hgIgnore = repo.getIgnore(); tikhomirov@59: TreeSet knownEntries = getDirstate().all(); tikhomirov@68: final boolean isTipBase; tikhomirov@68: if (baseRevision == TIP) { tikhomirov@68: baseRevision = repo.getManifest().getRevisionCount() - 1; tikhomirov@68: isTipBase = true; tikhomirov@68: } else { tikhomirov@68: isTipBase = baseRevision == repo.getManifest().getRevisionCount() - 1; tikhomirov@68: } 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@89: collect = new StatusCollector.ManifestRevisionInspector(); tikhomirov@59: repo.getManifest().walk(baseRevision, baseRevision, collect); tikhomirov@59: } tikhomirov@89: baseRevFiles = new TreeSet(collect.files()); tikhomirov@58: } tikhomirov@68: if (inspector instanceof StatusCollector.Record) { tikhomirov@68: StatusCollector sc = baseRevisionCollector == null ? new StatusCollector(repo) : baseRevisionCollector; tikhomirov@68: ((StatusCollector.Record) inspector).init(baseRevision, BAD_REVISION, sc); tikhomirov@68: } 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@90: checkLocalStatusAgainstBaseRevision(baseRevFiles, collect, baseRevision, 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@74: // missing known file from a working dir tikhomirov@59: if (getDirstate().checkRemoved(m) == null) { tikhomirov@74: // not removed from the repository = 'deleted' tikhomirov@58: inspector.missing(m); tikhomirov@74: } else { tikhomirov@74: // removed from the repo tikhomirov@76: // if we check against non-tip revision, do not report files that were added past that revision and now removed. tikhomirov@76: if (collect == null || baseRevFiles.contains(m)) { tikhomirov@76: inspector.removed(m); tikhomirov@76: } 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@74: inspector.copied(r.name2, fname); 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@90: private void checkLocalStatusAgainstBaseRevision(Set baseRevNames, ManifestRevisionInspector collect, int baseRevision, String fname, File f, StatusCollector.Inspector inspector) { tikhomirov@58: // fname is in the dirstate, either Normal, Added, Removed or Merged tikhomirov@89: Nodeid nid1 = collect.nodeid(fname); tikhomirov@89: String flags = collect.flags(fname); 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@90: if ((r = getDirstate().checkNormal(fname)) != null) { tikhomirov@90: String origin = StatusCollector.getOriginIfCopy(repo, fname, baseRevNames, baseRevision); tikhomirov@90: if (origin != null) { tikhomirov@90: inspector.copied(origin, fname); tikhomirov@90: return; tikhomirov@90: } tikhomirov@90: } else if ((r = getDirstate().checkAdded(fname)) != null) { tikhomirov@58: if (r.name2 != null && baseRevNames.contains(r.name2)) { tikhomirov@90: baseRevNames.remove(r.name2); // XXX surely I shall not report rename source as Removed? 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: }