tikhomirov@10: /*
tikhomirov@388: * Copyright (c) 2010-2012 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@234: import java.io.StringReader;
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@235: import org.tmatesoft.hg.core.Nodeid;
tikhomirov@295: import org.tmatesoft.hg.core.SessionContext;
tikhomirov@234: import org.tmatesoft.hg.internal.ByteArrayChannel;
tikhomirov@114: import org.tmatesoft.hg.internal.ConfigFile;
tikhomirov@74: import org.tmatesoft.hg.internal.DataAccessProvider;
tikhomirov@231: import org.tmatesoft.hg.internal.Experimental;
tikhomirov@114: import org.tmatesoft.hg.internal.Filter;
tikhomirov@407: import org.tmatesoft.hg.internal.Internals;
tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream;
tikhomirov@239: import org.tmatesoft.hg.internal.SubrepoManager;
tikhomirov@234: import org.tmatesoft.hg.util.CancelledException;
tikhomirov@235: import org.tmatesoft.hg.util.Pair;
tikhomirov@133: import org.tmatesoft.hg.util.Path;
tikhomirov@64: import org.tmatesoft.hg.util.PathRewrite;
tikhomirov@220: import org.tmatesoft.hg.util.ProgressSupport;
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@405: // IMPORTANT: if new constants added, consider fixing HgInternals#wrongRevisionIndex and HgInvalidRevisionException#getMessage
tikhomirov@405:
tikhomirov@405: /**
tikhomirov@405: * Revision index constant to indicate most recent revision
tikhomirov@405: */
tikhomirov@405: public static final int TIP = -3; // XXX TIP_REVISION?
tikhomirov@405:
tikhomirov@405: /**
tikhomirov@405: * Revision index constant to indicate invalid revision index value.
tikhomirov@405: * Primary use is default/uninitialized values where user input is expected and as return value where
tikhomirov@405: * an exception (e.g. {@link HgInvalidRevisionException}) is not desired
tikhomirov@405: */
tikhomirov@403: public static final int BAD_REVISION = Integer.MIN_VALUE; // XXX INVALID_REVISION?
tikhomirov@405:
tikhomirov@405: /**
tikhomirov@405: * Revision index constant to indicate working copy
tikhomirov@405: */
tikhomirov@405: public static final int WORKING_COPY = -2; // XXX WORKING_COPY_REVISION?
tikhomirov@252:
tikhomirov@405: /**
tikhomirov@423: * Constant ({@value #NO_REVISION}) to indicate revision absence or a fictitious revision of an empty repository.
tikhomirov@423: *
tikhomirov@423: *
Revision absence is vital e.g. for missing parent from {@link HgChangelog#parents(int, int[], byte[], byte[])} call and
tikhomirov@423: * to report cases when changeset records no corresponding manifest
tikhomirov@423: * revision {@link HgManifest#walk(int, int, org.tmatesoft.hg.repo.HgManifest.Inspector)}.
tikhomirov@423: *
tikhomirov@423: *
Use as imaginary revision/empty repository is handy as an argument (contrary to {@link #BAD_REVISION})
tikhomirov@423: * e.g in a status operation to visit changes from the very beginning of a repository.
tikhomirov@405: */
tikhomirov@405: public static final int NO_REVISION = -1;
tikhomirov@405:
tikhomirov@405: /**
tikhomirov@405: * Name of the primary branch, "default".
tikhomirov@405: */
tikhomirov@252: public static final String DEFAULT_BRANCH_NAME = "default";
tikhomirov@5:
tikhomirov@2: // temp aux marker method
tikhomirov@2: public static IllegalStateException notImplemented() {
tikhomirov@2: return new IllegalStateException("Not implemented");
tikhomirov@2: }
tikhomirov@148:
tikhomirov@74: private final File repoDir; // .hg folder
tikhomirov@237: private final File workingDir; // .hg/../
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@295: private final SessionContext sessionContext;
tikhomirov@74:
tikhomirov@97: private HgChangelog changelog;
tikhomirov@2: private HgManifest manifest;
tikhomirov@50: private HgTags tags;
tikhomirov@220: private HgBranches branches;
tikhomirov@231: private HgMergeState mergeState;
tikhomirov@239: private SubrepoManager subRepos;
tikhomirov@220:
tikhomirov@74: // XXX perhaps, shall enable caching explicitly
tikhomirov@74: private final HashMap> streamsCache = new HashMap>();
tikhomirov@74:
tikhomirov@388: private final org.tmatesoft.hg.internal.Internals impl;
tikhomirov@91: private HgIgnore ignore;
tikhomirov@331: private HgRepoConfig repoConfig;
tikhomirov@220:
tikhomirov@430: /*
tikhomirov@430: * TODO [post-1.0] move to a better place, e.g. WorkingCopy container that tracks both dirstate and branches
tikhomirov@430: * (and, perhaps, undo, lastcommit and other similar information), and is change listener so that we don't need to
tikhomirov@430: * worry about this cached value become stale
tikhomirov@430: */
tikhomirov@430: private String wcBranch;
tikhomirov@430:
tikhomirov@430:
tikhomirov@74: HgRepository(String repositoryPath) {
tikhomirov@74: repoDir = null;
tikhomirov@237: workingDir = null;
tikhomirov@74: repoLocation = repositoryPath;
tikhomirov@74: dataAccess = null;
tikhomirov@74: dataPathHelper = repoPathHelper = null;
tikhomirov@142: normalizePath = null;
tikhomirov@295: sessionContext = null;
tikhomirov@388: impl = null;
tikhomirov@1: }
tikhomirov@1:
tikhomirov@425: /**
tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception
tikhomirov@425: */
tikhomirov@425: HgRepository(SessionContext ctx, String repositoryPath, File repositoryRoot) throws HgRuntimeException {
tikhomirov@74: assert ".hg".equals(repositoryRoot.getName()) && repositoryRoot.isDirectory();
tikhomirov@148: assert repositoryPath != null;
tikhomirov@148: assert repositoryRoot != null;
tikhomirov@295: assert ctx != null;
tikhomirov@74: repoDir = repositoryRoot;
tikhomirov@237: workingDir = repoDir.getParentFile();
tikhomirov@237: if (workingDir == null) {
tikhomirov@237: throw new IllegalArgumentException(repoDir.toString());
tikhomirov@237: }
tikhomirov@388: impl = new org.tmatesoft.hg.internal.Internals(ctx);
tikhomirov@148: repoLocation = repositoryPath;
tikhomirov@295: sessionContext = ctx;
tikhomirov@295: dataAccess = new DataAccessProvider(ctx);
tikhomirov@295: impl.parseRequires(this, new File(repoDir, "requires"));
tikhomirov@388: normalizePath = impl.buildNormalizePathRewrite();
tikhomirov@74: dataPathHelper = impl.buildDataFilesHelper();
tikhomirov@74: repoPathHelper = impl.buildRepositoryFilesHelper();
tikhomirov@1: }
tikhomirov@0:
tikhomirov@145: @Override
tikhomirov@145: public String toString() {
tikhomirov@145: return getClass().getSimpleName() + "[" + getLocation() + (isInvalid() ? "(BAD)" : "") + "]";
tikhomirov@145: }
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@388: if (changelog == null) {
tikhomirov@292: CharSequence storagePath = repoPathHelper.rewrite("00changelog.i");
tikhomirov@202: RevlogStream content = resolve(Path.create(storagePath), true);
tikhomirov@388: changelog = new HgChangelog(this, content);
tikhomirov@0: }
tikhomirov@388: return changelog;
tikhomirov@0: }
tikhomirov@2:
tikhomirov@74: public HgManifest getManifest() {
tikhomirov@388: if (manifest == null) {
tikhomirov@202: RevlogStream content = resolve(Path.create(repoPathHelper.rewrite("00manifest.i")), true);
tikhomirov@412: manifest = new HgManifest(this, content, impl.buildFileNameEncodingHelper());
tikhomirov@2: }
tikhomirov@388: return manifest;
tikhomirov@2: }
tikhomirov@50:
tikhomirov@318: public HgTags getTags() throws HgInvalidControlFileException {
tikhomirov@50: if (tags == null) {
tikhomirov@234: tags = new HgTags(this);
tikhomirov@318: HgDataFile hgTags = getFileNode(".hgtags");
tikhomirov@318: if (hgTags.exists()) {
tikhomirov@418: for (int i = 0; i <= hgTags.getLastRevision(); i++) { // TODO post-1.0 in fact, would be handy to have walk(start,end)
tikhomirov@318: // method for data files as well, though it looks odd.
tikhomirov@318: try {
tikhomirov@318: ByteArrayChannel sink = new ByteArrayChannel();
tikhomirov@318: hgTags.content(i, sink);
tikhomirov@318: final String content = new String(sink.toArray(), "UTF8");
tikhomirov@318: tags.readGlobal(new StringReader(content));
tikhomirov@318: } catch (CancelledException ex) {
tikhomirov@318: // IGNORE, can't happen, we did not configure cancellation
tikhomirov@318: getContext().getLog().debug(getClass(), ex, null);
tikhomirov@318: } catch (IOException ex) {
tikhomirov@318: // UnsupportedEncodingException can't happen (UTF8)
tikhomirov@318: // only from readGlobal. Need to reconsider exceptions thrown from there:
tikhomirov@318: // BufferedReader wraps String and unlikely to throw IOException, perhaps, log is enough?
tikhomirov@318: getContext().getLog().error(getClass(), ex, null);
tikhomirov@318: // XXX need to decide what to do this. failure to read single revision shall not break complete cycle
tikhomirov@234: }
tikhomirov@234: }
tikhomirov@318: }
tikhomirov@318: File file2read = null;
tikhomirov@318: try {
tikhomirov@318: file2read = new File(getWorkingDir(), ".hgtags");
tikhomirov@318: tags.readGlobal(file2read); // XXX replace with HgDataFile.workingCopy
tikhomirov@318: file2read = new File(repoDir, "localtags");
tikhomirov@318: tags.readLocal(file2read);
tikhomirov@104: } catch (IOException ex) {
tikhomirov@295: getContext().getLog().error(getClass(), ex, null);
tikhomirov@318: throw new HgInvalidControlFileException("Failed to read tags", ex, file2read);
tikhomirov@104: }
tikhomirov@50: }
tikhomirov@50: return tags;
tikhomirov@50: }
tikhomirov@50:
tikhomirov@366: public HgBranches getBranches() throws HgInvalidControlFileException {
tikhomirov@220: if (branches == null) {
tikhomirov@220: branches = new HgBranches(this);
tikhomirov@220: branches.collect(ProgressSupport.Factory.get(null));
tikhomirov@220: }
tikhomirov@220: return branches;
tikhomirov@220: }
tikhomirov@231:
tikhomirov@231: @Experimental(reason="Perhaps, shall not cache instance, and provide loadMergeState as it may change often")
tikhomirov@231: public HgMergeState getMergeState() {
tikhomirov@231: if (mergeState == null) {
tikhomirov@231: mergeState = new HgMergeState(this);
tikhomirov@231: }
tikhomirov@231: return mergeState;
tikhomirov@231: }
tikhomirov@220:
tikhomirov@74: public HgDataFile getFileNode(String path) {
tikhomirov@292: CharSequence nPath = normalizePath.rewrite(path);
tikhomirov@292: CharSequence storagePath = dataPathHelper.rewrite(nPath);
tikhomirov@202: RevlogStream content = resolve(Path.create(storagePath), false);
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@292: CharSequence storagePath = dataPathHelper.rewrite(path.toString());
tikhomirov@202: RevlogStream content = resolve(Path.create(storagePath), false);
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@284:
tikhomirov@284: /**
tikhomirov@348: * @return pair of values, {@link Pair#first()} and {@link Pair#second()} are respective parents, never null
.
tikhomirov@348: * @throws HgInvalidControlFileException if attempt to read information about working copy parents from dirstate failed
tikhomirov@284: */
tikhomirov@348: public Pair getWorkingCopyParents() throws HgInvalidControlFileException {
tikhomirov@284: return HgDirstate.readParents(this, new File(repoDir, "dirstate"));
tikhomirov@235: }
tikhomirov@252:
tikhomirov@252: /**
tikhomirov@252: * @return name of the branch associated with working directory, never null
.
tikhomirov@348: * @throws HgInvalidControlFileException if attempt to read branch name failed.
tikhomirov@252: */
tikhomirov@348: public String getWorkingCopyBranchName() throws HgInvalidControlFileException {
tikhomirov@430: if (wcBranch == null) {
tikhomirov@430: wcBranch = HgDirstate.readBranch(this, new File(repoDir, "branch"));
tikhomirov@430: }
tikhomirov@430: return wcBranch;
tikhomirov@252: }
tikhomirov@2:
tikhomirov@237: /**
tikhomirov@237: * @return location where user files (shall) reside
tikhomirov@237: */
tikhomirov@237: public File getWorkingDir() {
tikhomirov@237: return workingDir;
tikhomirov@237: }
tikhomirov@239:
tikhomirov@239: /**
tikhomirov@239: * Provides access to sub-repositories defined in this repository. Enumerated sub-repositories are those directly
tikhomirov@239: * known, not recursive collection of all nested sub-repositories.
tikhomirov@239: * @return list of all known sub-repositories in this repository, or empty list if none found.
tikhomirov@239: */
tikhomirov@348: public List getSubrepositories() throws HgInvalidControlFileException {
tikhomirov@239: if (subRepos == null) {
tikhomirov@239: subRepos = new SubrepoManager(this);
tikhomirov@348: subRepos.read();
tikhomirov@239: }
tikhomirov@239: return subRepos.all();
tikhomirov@239: }
tikhomirov@237:
tikhomirov@331:
tikhomirov@331: public HgRepoConfig getConfiguration() /* XXX throws HgInvalidControlFileException? Description of the exception suggests it is only for files under ./hg/*/ {
tikhomirov@331: if (repoConfig == null) {
tikhomirov@331: try {
tikhomirov@331: ConfigFile configFile = impl.readConfiguration(this, getRepositoryRoot());
tikhomirov@331: repoConfig = new HgRepoConfig(configFile);
tikhomirov@331: } catch (IOException ex) {
tikhomirov@331: String m = "Errors while reading user configuration file";
tikhomirov@331: getContext().getLog().warn(getClass(), ex, m);
tikhomirov@331: return new HgRepoConfig(new ConfigFile()); // empty config, do not cache, allow to try once again
tikhomirov@331: //throw new HgInvalidControlFileException(m, ex, null);
tikhomirov@331: }
tikhomirov@331: }
tikhomirov@331: return repoConfig;
tikhomirov@331: }
tikhomirov@331:
tikhomirov@237: // shall be of use only for internal classes
tikhomirov@74: /*package-local*/ File getRepositoryRoot() {
tikhomirov@74: return repoDir;
tikhomirov@74: }
tikhomirov@337:
tikhomirov@337: /*package-local, debug*/String getStoragePath(HgDataFile df) {
tikhomirov@418: // may come handy for debug
tikhomirov@337: return dataPathHelper.rewrite(df.getPath().toString()).toString();
tikhomirov@337: }
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@280: // XXX consider passing Path pool or factory to produce (shared) Path instead of Strings
tikhomirov@431: /*package-local*/ final HgDirstate loadDirstate(Path.Source pathFactory) throws HgInvalidControlFileException {
tikhomirov@292: PathRewrite canonicalPath = null;
tikhomirov@388: if (!impl.isCaseSensitiveFileSystem()) {
tikhomirov@292: canonicalPath = new PathRewrite() {
tikhomirov@292:
tikhomirov@292: public CharSequence rewrite(CharSequence path) {
tikhomirov@292: return path.toString().toLowerCase();
tikhomirov@292: }
tikhomirov@292: };
tikhomirov@292: }
tikhomirov@431: HgDirstate ds = new HgDirstate(this, new File(repoDir, "dirstate"), pathFactory, canonicalPath);
tikhomirov@412: ds.read(impl.buildFileNameEncodingHelper());
tikhomirov@348: return ds;
tikhomirov@74: }
tikhomirov@74:
tikhomirov@289: /**
tikhomirov@289: * Access to configured set of ignored files.
tikhomirov@289: * @see HgIgnore#isIgnored(Path)
tikhomirov@289: */
tikhomirov@335: public HgIgnore getIgnore() /*throws HgInvalidControlFileException */{
tikhomirov@91: // TODO read config for additional locations
tikhomirov@91: if (ignore == null) {
tikhomirov@409: ignore = new HgIgnore(getToRepoPathHelper());
tikhomirov@335: File ignoreFile = new File(getWorkingDir(), ".hgignore");
tikhomirov@91: try {
tikhomirov@335: final List errors = ignore.read(ignoreFile);
tikhomirov@335: if (errors != null) {
tikhomirov@407: getContext().getLog().warn(getClass(), "Syntax errors parsing .hgignore:\n%s", Internals.join(errors, ",\n"));
tikhomirov@335: }
tikhomirov@91: } catch (IOException ex) {
tikhomirov@335: final String m = "Error reading .hgignore file";
tikhomirov@335: getContext().getLog().warn(getClass(), ex, m);
tikhomirov@335: // throw new HgInvalidControlFileException(m, ex, ignoreFile);
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@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@202: /*package-local*/ RevlogStream resolve(Path path, boolean shallFakeNonExistent) {
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@388: if (impl.shallCacheRevlogs()) {
tikhomirov@388: streamsCache.put(path, new SoftReference(s));
tikhomirov@388: }
tikhomirov@74: return s;
tikhomirov@202: } else {
tikhomirov@202: if (shallFakeNonExistent) {
tikhomirov@202: try {
tikhomirov@202: File fake = File.createTempFile(f.getName(), null);
tikhomirov@202: fake.deleteOnExit();
tikhomirov@202: return new RevlogStream(dataAccess, fake);
tikhomirov@202: } catch (IOException ex) {
tikhomirov@295: getContext().getLog().info(getClass(), ex, null);
tikhomirov@202: }
tikhomirov@202: }
tikhomirov@74: }
tikhomirov@74: return null; // XXX empty stream instead?
tikhomirov@74: }
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@237:
tikhomirov@237: /*package-local*/ File getFile(HgDataFile dataFile) {
tikhomirov@237: return new File(getWorkingDir(), dataFile.getPath().toString());
tikhomirov@237: }
tikhomirov@295:
tikhomirov@295: /*package-local*/ SessionContext getContext() {
tikhomirov@295: return sessionContext;
tikhomirov@295: }
tikhomirov@412:
tikhomirov@412: /*package-local*/ Internals getImplHelper() {
tikhomirov@412: return impl;
tikhomirov@412: }
tikhomirov@114:
tikhomirov@114: private List instantiateFilters(Path p, Filter.Options opts) {
tikhomirov@331: List factories = impl.getFilters(this);
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@0: }