tikhomirov@10: /* tikhomirov@74: * Copyright (c) 2010-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@0: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@74: tikhomirov@74: import java.io.File; tikhomirov@74: import java.io.IOException; tikhomirov@74: import java.lang.ref.SoftReference; tikhomirov@74: import java.util.HashMap; tikhomirov@0: tikhomirov@64: import org.tmatesoft.hg.core.Path; tikhomirov@74: import org.tmatesoft.hg.internal.DataAccessProvider; tikhomirov@74: import org.tmatesoft.hg.internal.RequiresFile; tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream; tikhomirov@74: import org.tmatesoft.hg.util.FileWalker; tikhomirov@64: import org.tmatesoft.hg.util.PathRewrite; tikhomirov@64: tikhomirov@1: tikhomirov@74: tikhomirov@0: /** tikhomirov@64: * Shall be as state-less as possible, all the caching happens outside the repo, in commands/walkers tikhomirov@74: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@0: */ tikhomirov@74: public final class HgRepository { tikhomirov@0: tikhomirov@5: public static final int TIP = -1; tikhomirov@68: public static final int BAD_REVISION = Integer.MIN_VALUE; tikhomirov@68: public static final int WORKING_COPY = -2; tikhomirov@5: tikhomirov@2: // temp aux marker method tikhomirov@2: public static IllegalStateException notImplemented() { tikhomirov@2: return new IllegalStateException("Not implemented"); tikhomirov@2: } tikhomirov@2: tikhomirov@74: private final File repoDir; // .hg folder tikhomirov@74: private final String repoLocation; tikhomirov@74: private final DataAccessProvider dataAccess; tikhomirov@74: private final PathRewrite normalizePath = new PathRewrite() { tikhomirov@74: tikhomirov@74: public String rewrite(String path) { tikhomirov@74: // TODO handle . and .. (although unlikely to face them from GUI client) tikhomirov@74: path = path.replace('\\', '/').replace("//", "/"); tikhomirov@74: if (path.startsWith("/")) { tikhomirov@74: path = path.substring(1); tikhomirov@74: } tikhomirov@74: return path; tikhomirov@74: } tikhomirov@74: }; tikhomirov@74: private final PathRewrite dataPathHelper; tikhomirov@74: private final PathRewrite repoPathHelper; tikhomirov@74: tikhomirov@0: private Changelog changelog; tikhomirov@2: private HgManifest manifest; tikhomirov@50: private HgTags tags; tikhomirov@74: // XXX perhaps, shall enable caching explicitly tikhomirov@74: private final HashMap> streamsCache = new HashMap>(); tikhomirov@74: tikhomirov@74: private final org.tmatesoft.hg.internal.Internals impl = new org.tmatesoft.hg.internal.Internals(); tikhomirov@91: private HgIgnore ignore; tikhomirov@2: tikhomirov@74: HgRepository(String repositoryPath) { tikhomirov@74: repoDir = null; tikhomirov@74: repoLocation = repositoryPath; tikhomirov@74: dataAccess = null; tikhomirov@74: dataPathHelper = repoPathHelper = null; tikhomirov@1: } tikhomirov@1: tikhomirov@74: HgRepository(File repositoryRoot) throws IOException { tikhomirov@74: assert ".hg".equals(repositoryRoot.getName()) && repositoryRoot.isDirectory(); tikhomirov@74: repoDir = repositoryRoot; tikhomirov@74: repoLocation = repositoryRoot.getParentFile().getCanonicalPath(); tikhomirov@74: dataAccess = new DataAccessProvider(); tikhomirov@74: parseRequires(); tikhomirov@74: dataPathHelper = impl.buildDataFilesHelper(); tikhomirov@74: repoPathHelper = impl.buildRepositoryFilesHelper(); tikhomirov@1: } tikhomirov@0: tikhomirov@74: tikhomirov@74: public String getLocation() { tikhomirov@74: return repoLocation; tikhomirov@74: } tikhomirov@74: tikhomirov@74: public boolean isInvalid() { tikhomirov@74: return repoDir == null || !repoDir.exists() || !repoDir.isDirectory(); tikhomirov@74: } tikhomirov@74: tikhomirov@74: public Changelog getChangelog() { tikhomirov@0: if (this.changelog == null) { tikhomirov@74: String storagePath = repoPathHelper.rewrite("00changelog.i"); tikhomirov@74: RevlogStream content = resolve(Path.create(storagePath)); tikhomirov@3: this.changelog = new Changelog(this, content); tikhomirov@0: } tikhomirov@0: return this.changelog; tikhomirov@0: } tikhomirov@2: tikhomirov@74: public HgManifest getManifest() { tikhomirov@2: if (this.manifest == null) { tikhomirov@74: RevlogStream content = resolve(Path.create(repoPathHelper.rewrite("00manifest.i"))); tikhomirov@13: this.manifest = new HgManifest(this, content); tikhomirov@2: } tikhomirov@2: return this.manifest; tikhomirov@2: } tikhomirov@50: tikhomirov@50: public final HgTags getTags() { tikhomirov@50: if (tags == null) { tikhomirov@74: tags = new HgTags(); tikhomirov@50: } tikhomirov@50: return tags; tikhomirov@50: } tikhomirov@50: tikhomirov@74: public HgDataFile getFileNode(String path) { tikhomirov@74: String nPath = normalizePath.rewrite(path); tikhomirov@74: String storagePath = dataPathHelper.rewrite(nPath); tikhomirov@77: RevlogStream content = resolve(Path.create(storagePath)); tikhomirov@77: return new HgDataFile(this, Path.create(nPath), content); tikhomirov@74: } tikhomirov@1: tikhomirov@74: public HgDataFile getFileNode(Path path) { tikhomirov@77: String storagePath = dataPathHelper.rewrite(path.toString()); tikhomirov@77: RevlogStream content = resolve(Path.create(storagePath)); tikhomirov@74: // XXX no content when no file? or HgDataFile.exists() to detect that? How about files that were removed in previous releases? tikhomirov@74: return new HgDataFile(this, path, content); tikhomirov@74: } tikhomirov@2: tikhomirov@74: public PathRewrite getPathHelper() { // Really need to be public? tikhomirov@74: return normalizePath; tikhomirov@74: } tikhomirov@2: tikhomirov@74: /*package-local*/ File getRepositoryRoot() { tikhomirov@74: return repoDir; tikhomirov@74: } tikhomirov@74: tikhomirov@74: // XXX package-local, unless there are cases when required from outside (guess, working dir/revision walkers may hide dirstate access and no public visibility needed) tikhomirov@74: /*package-local*/ final HgDirstate loadDirstate() { tikhomirov@74: return new HgDirstate(getDataAccess(), new File(repoDir, "dirstate")); tikhomirov@74: } tikhomirov@74: tikhomirov@74: // package-local, see comment for loadDirstate tikhomirov@91: /*package-local*/ final HgIgnore getIgnore() { tikhomirov@91: // TODO read config for additional locations tikhomirov@91: if (ignore == null) { tikhomirov@91: ignore = new HgIgnore(); tikhomirov@91: try { tikhomirov@91: File ignoreFile = new File(repoDir.getParentFile(), ".hgignore"); tikhomirov@91: ignore.read(ignoreFile); tikhomirov@91: } catch (IOException ex) { tikhomirov@91: ex.printStackTrace(); // log warn tikhomirov@91: } tikhomirov@91: } tikhomirov@91: return ignore; tikhomirov@74: } tikhomirov@74: tikhomirov@74: /*package-local*/ DataAccessProvider getDataAccess() { tikhomirov@74: return dataAccess; tikhomirov@74: } tikhomirov@74: tikhomirov@74: // FIXME not sure repository shall create walkers tikhomirov@74: /*package-local*/ FileWalker createWorkingDirWalker() { tikhomirov@74: return new FileWalker(repoDir.getParentFile()); tikhomirov@74: } tikhomirov@9: tikhomirov@2: /** tikhomirov@2: * Perhaps, should be separate interface, like ContentLookup tikhomirov@74: * path - repository storage path (i.e. one usually with .i or .d) tikhomirov@2: */ tikhomirov@74: /*package-local*/ RevlogStream resolve(Path path) { tikhomirov@74: final SoftReference ref = streamsCache.get(path); tikhomirov@74: RevlogStream cached = ref == null ? null : ref.get(); tikhomirov@74: if (cached != null) { tikhomirov@74: return cached; tikhomirov@74: } tikhomirov@74: File f = new File(repoDir, path.toString()); tikhomirov@74: if (f.exists()) { tikhomirov@74: RevlogStream s = new RevlogStream(dataAccess, f); tikhomirov@74: streamsCache.put(path, new SoftReference(s)); tikhomirov@74: return s; tikhomirov@74: } tikhomirov@74: return null; // XXX empty stream instead? tikhomirov@74: } tikhomirov@74: tikhomirov@74: private void parseRequires() { tikhomirov@74: new RequiresFile().parse(impl, new File(repoDir, "requires")); tikhomirov@74: } tikhomirov@0: }