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@102: * contact TMate Software at support@hg4j.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@114: import java.util.ArrayList; tikhomirov@114: import java.util.Collections; tikhomirov@74: import java.util.HashMap; tikhomirov@114: import java.util.List; tikhomirov@0: tikhomirov@114: import org.tmatesoft.hg.internal.ConfigFile; tikhomirov@74: import org.tmatesoft.hg.internal.DataAccessProvider; tikhomirov@114: import org.tmatesoft.hg.internal.Filter; tikhomirov@141: import org.tmatesoft.hg.internal.RelativePathRewrite; tikhomirov@74: import org.tmatesoft.hg.internal.RequiresFile; tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream; tikhomirov@141: import org.tmatesoft.hg.util.FileIterator; tikhomirov@74: import org.tmatesoft.hg.util.FileWalker; tikhomirov@133: import org.tmatesoft.hg.util.Path; 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@142: private final PathRewrite normalizePath; tikhomirov@74: private final PathRewrite dataPathHelper; tikhomirov@74: private final PathRewrite repoPathHelper; tikhomirov@74: tikhomirov@97: private HgChangelog 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@114: private ConfigFile configFile; 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@142: normalizePath = 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@142: final boolean runningOnWindows = System.getProperty("os.name").indexOf("Windows") != -1; tikhomirov@142: if (runningOnWindows) { tikhomirov@142: normalizePath = new PathRewrite() { tikhomirov@142: tikhomirov@142: public String rewrite(String path) { tikhomirov@142: // TODO handle . and .. (although unlikely to face them from GUI client) tikhomirov@142: path = path.replace('\\', '/').replace("//", "/"); tikhomirov@142: if (path.startsWith("/")) { tikhomirov@142: path = path.substring(1); tikhomirov@142: } tikhomirov@142: return path; tikhomirov@142: } tikhomirov@142: }; tikhomirov@142: } else { tikhomirov@142: normalizePath = new PathRewrite.Empty(); // or strip leading slash, perhaps? tikhomirov@142: } 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@97: public HgChangelog getChangelog() { tikhomirov@0: if (this.changelog == null) { tikhomirov@74: String storagePath = repoPathHelper.rewrite("00changelog.i"); tikhomirov@74: RevlogStream content = resolve(Path.create(storagePath)); tikhomirov@97: this.changelog = new HgChangelog(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@104: try { tikhomirov@104: tags.readGlobal(new File(repoDir.getParentFile(), ".hgtags")); tikhomirov@104: tags.readLocal(new File(repoDir, "localtags")); tikhomirov@104: } catch (IOException ex) { tikhomirov@104: ex.printStackTrace(); // FIXME log or othewise report tikhomirov@104: } 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@115: Path p = Path.create(nPath); tikhomirov@115: if (content == null) { tikhomirov@115: return new HgDataFile(this, p); tikhomirov@115: } tikhomirov@115: return new HgDataFile(this, p, 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@115: // XXX no content when no file? or HgDataFile.exists() to detect that? tikhomirov@115: if (content == null) { tikhomirov@115: return new HgDataFile(this, path); tikhomirov@115: } tikhomirov@74: return new HgDataFile(this, path, content); tikhomirov@74: } tikhomirov@2: tikhomirov@142: /* clients need to rewrite path from their FS to a repository-friendly paths, and, perhaps, vice versa*/ tikhomirov@142: public PathRewrite getToRepoPathHelper() { tikhomirov@74: return normalizePath; tikhomirov@74: } tikhomirov@2: tikhomirov@114: // local to hide use of io.File. 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@141: /*package-local*/ FileIterator createWorkingDirWalker() { tikhomirov@141: File repoRoot = repoDir.getParentFile(); tikhomirov@142: Path.Source pathSrc = new Path.SimpleSource(new PathRewrite.Composite(new RelativePathRewrite(repoRoot), getToRepoPathHelper())); tikhomirov@141: // Impl note: simple source is enough as files in the working dir are all unique tikhomirov@141: // even if they might get reused (i.e. after FileIterator#reset() and walking once again), tikhomirov@141: // path caching is better to be done in the code which knows that path are being reused tikhomirov@141: return new FileWalker(repoRoot, pathSrc); 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@114: tikhomirov@114: // can't expose internal class, otherwise seems reasonable to have it in API tikhomirov@114: /*package-local*/ ConfigFile getConfigFile() { tikhomirov@114: if (configFile == null) { tikhomirov@114: configFile = impl.newConfigFile(); tikhomirov@114: configFile.addLocation(new File(System.getProperty("user.home"), ".hgrc")); tikhomirov@114: // last one, overrides anything else tikhomirov@114: // /.hg/hgrc tikhomirov@114: configFile.addLocation(new File(getRepositoryRoot(), "hgrc")); tikhomirov@114: } tikhomirov@114: return configFile; tikhomirov@114: } tikhomirov@114: tikhomirov@114: /*package-local*/ List getFiltersFromRepoToWorkingDir(Path p) { tikhomirov@114: return instantiateFilters(p, new Filter.Options(Filter.Direction.FromRepo)); tikhomirov@114: } tikhomirov@114: tikhomirov@114: /*package-local*/ List getFiltersFromWorkingDirToRepo(Path p) { tikhomirov@114: return instantiateFilters(p, new Filter.Options(Filter.Direction.ToRepo)); tikhomirov@114: } tikhomirov@114: tikhomirov@114: private List instantiateFilters(Path p, Filter.Options opts) { tikhomirov@114: List factories = impl.getFilters(this, getConfigFile()); tikhomirov@114: if (factories.isEmpty()) { tikhomirov@114: return Collections.emptyList(); tikhomirov@114: } tikhomirov@114: ArrayList rv = new ArrayList(factories.size()); tikhomirov@114: for (Filter.Factory ff : factories) { tikhomirov@114: Filter f = ff.create(p, opts); tikhomirov@114: if (f != null) { tikhomirov@114: rv.add(f); tikhomirov@114: } tikhomirov@114: } tikhomirov@114: return rv; tikhomirov@114: } tikhomirov@74: tikhomirov@74: private void parseRequires() { tikhomirov@74: new RequiresFile().parse(impl, new File(repoDir, "requires")); tikhomirov@74: } tikhomirov@114: tikhomirov@0: }