tikhomirov@59: /* tikhomirov@409: * Copyright (c) 2011-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@59: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@74: tikhomirov@148: import static org.tmatesoft.hg.repo.HgRepository.*; tikhomirov@148: tikhomirov@269: import java.io.BufferedReader; tikhomirov@114: import java.io.File; tikhomirov@269: import java.io.IOException; tikhomirov@269: import java.io.Reader; tikhomirov@128: import java.net.InetAddress; tikhomirov@128: import java.net.UnknownHostException; tikhomirov@114: tikhomirov@442: import org.tmatesoft.hg.core.Nodeid; tikhomirov@229: import org.tmatesoft.hg.internal.Experimental; tikhomirov@409: import org.tmatesoft.hg.internal.Internals; tikhomirov@229: import org.tmatesoft.hg.internal.RelativePathRewrite; tikhomirov@409: import org.tmatesoft.hg.internal.WinToNixPathRewrite; tikhomirov@442: import org.tmatesoft.hg.repo.HgSubrepoLocation.Kind; tikhomirov@229: import org.tmatesoft.hg.util.FileIterator; tikhomirov@229: import org.tmatesoft.hg.util.FileWalker; tikhomirov@142: import org.tmatesoft.hg.util.Path; tikhomirov@229: import org.tmatesoft.hg.util.PathRewrite; tikhomirov@114: tikhomirov@59: tikhomirov@59: /** tikhomirov@59: * DO NOT USE THIS CLASS, INTENDED FOR TESTING PURPOSES. tikhomirov@59: * tikhomirov@442: *
This class is not part of the public API and may change or vanish any moment. tikhomirov@442: * tikhomirov@442: *
This class gives access to repository internals, and holds methods that I'm not confident have to be widely accessible
tikhomirov@59: * Debug helper, to access otherwise restricted (package-local) methods
tikhomirov@59: *
tikhomirov@74: * @author Artem Tikhomirov
tikhomirov@74: * @author TMate Software Ltd.
tikhomirov@59: */
tikhomirov@229: @Experimental(reason="Perhaps, shall split methods with debug purpose from methods that are experimental API")
tikhomirov@96: public class HgInternals {
tikhomirov@59:
tikhomirov@59: private final HgRepository repo;
tikhomirov@59:
tikhomirov@96: public HgInternals(HgRepository hgRepo) {
tikhomirov@74: repo = hgRepo;
tikhomirov@59: }
tikhomirov@59:
tikhomirov@348: public HgDirstate getDirstate() throws HgInvalidControlFileException {
tikhomirov@431: return repo.loadDirstate(new Path.SimpleSource());
tikhomirov@296: }
tikhomirov@296:
tikhomirov@296: // tests
tikhomirov@348: public HgDirstate createDirstate(boolean caseSensitiveFileSystem) throws HgInvalidControlFileException {
tikhomirov@296: PathRewrite canonicalPath = null;
tikhomirov@296: if (!caseSensitiveFileSystem) {
tikhomirov@296: canonicalPath = new PathRewrite() {
tikhomirov@296:
tikhomirov@296: public CharSequence rewrite(CharSequence path) {
tikhomirov@296: return path.toString().toLowerCase();
tikhomirov@296: }
tikhomirov@296: };
tikhomirov@296: }
tikhomirov@490: HgDirstate ds = new HgDirstate(repo.getImplHelper(), new Path.SimpleSource(), canonicalPath);
tikhomirov@490: ds.read();
tikhomirov@348: return ds;
tikhomirov@296: }
tikhomirov@296:
tikhomirov@296: public Path[] checkKnown(HgDirstate dirstate, Path[] toCheck) {
tikhomirov@296: Path[] rv = new Path[toCheck.length];
tikhomirov@296: for (int i = 0; i < toCheck.length; i++) {
tikhomirov@296: rv[i] = dirstate.known(toCheck[i]);
tikhomirov@296: }
tikhomirov@296: return rv;
tikhomirov@59: }
tikhomirov@442:
tikhomirov@442: public HgSubrepoLocation newSubrepo(Path loc, String src, Kind kind, Nodeid rev) {
tikhomirov@442: return new HgSubrepoLocation(repo, loc, src, kind, rev);
tikhomirov@442: }
tikhomirov@493:
tikhomirov@493: public static Internals getImplementationRepo(HgRepository hgRepo) {
tikhomirov@493: return hgRepo.getImplHelper();
tikhomirov@493: }
tikhomirov@59:
tikhomirov@409: /**
tikhomirov@409: * @param source where to read definitions from
tikhomirov@409: * @param globPathRewrite null
to use default, or pass an instance to override defaults
tikhomirov@409: * @return
tikhomirov@409: * @throws IOException
tikhomirov@409: */
tikhomirov@409: public static HgIgnore newHgIgnore(Reader source, PathRewrite globPathRewrite) throws IOException {
tikhomirov@409: if (globPathRewrite == null) {
tikhomirov@409: // shall match that of HgRepository#getIgnore() (Internals#buildNormalizePathRewrite())
tikhomirov@409: if (Internals.runningOnWindows()) {
tikhomirov@409: globPathRewrite = new WinToNixPathRewrite();
tikhomirov@409: } else {
tikhomirov@409: globPathRewrite = new PathRewrite.Empty();
tikhomirov@409: }
tikhomirov@409: }
tikhomirov@409: HgIgnore hgIgnore = new HgIgnore(globPathRewrite);
tikhomirov@269: BufferedReader br = source instanceof BufferedReader ? (BufferedReader) source : new BufferedReader(source);
tikhomirov@269: hgIgnore.read(br);
tikhomirov@269: br.close();
tikhomirov@269: return hgIgnore;
tikhomirov@269: }
tikhomirov@128:
tikhomirov@128: // in fact, need a setter for this anyway, shall move to internal.Internals perhaps?
tikhomirov@128: public String getNextCommitUsername() {
tikhomirov@128: String hgUser = System.getenv("HGUSER");
tikhomirov@128: if (hgUser != null && hgUser.trim().length() > 0) {
tikhomirov@128: return hgUser.trim();
tikhomirov@128: }
tikhomirov@331: String configValue = repo.getConfiguration().getStringValue("ui", "username", null);
tikhomirov@128: if (configValue != null) {
tikhomirov@128: return configValue;
tikhomirov@128: }
tikhomirov@128: String email = System.getenv("EMAIL");
tikhomirov@128: if (email != null && email.trim().length() > 0) {
tikhomirov@128: return email;
tikhomirov@128: }
tikhomirov@128: String username = System.getProperty("user.name");
tikhomirov@128: try {
tikhomirov@128: String hostname = InetAddress.getLocalHost().getHostName();
tikhomirov@128: return username + '@' + hostname;
tikhomirov@128: } catch (UnknownHostException ex) {
tikhomirov@128: return username;
tikhomirov@128: }
tikhomirov@128: }
tikhomirov@229:
tikhomirov@229: /*package-local*/ FileIterator createWorkingDirWalker(Path.Matcher workindDirScope) {
tikhomirov@237: File repoRoot = repo.getWorkingDir();
tikhomirov@229: Path.Source pathSrc = new Path.SimpleSource(new PathRewrite.Composite(new RelativePathRewrite(repoRoot), repo.getToRepoPathHelper()));
tikhomirov@229: // Impl note: simple source is enough as files in the working dir are all unique
tikhomirov@229: // even if they might get reused (i.e. after FileIterator#reset() and walking once again),
tikhomirov@229: // path caching is better to be done in the code which knows that path are being reused
tikhomirov@490: return new FileWalker(repo.getSessionContext(), repoRoot, pathSrc, workindDirScope);
tikhomirov@229: }
tikhomirov@295:
tikhomirov@368: // Convenient check of revision index for validity (not all negative values are wrong as long as we use negative constants)
tikhomirov@425: public static boolean wrongRevisionIndex(int rev) {
tikhomirov@425: // TODO Another method to check,throw and expand TIP at once (check[Revision|Revlog]Index()
tikhomirov@405: return rev < 0 && rev != TIP && rev != WORKING_COPY && rev != BAD_REVISION && rev != NO_REVISION;
tikhomirov@148: }
tikhomirov@403:
tikhomirov@347: // throws HgInvalidRevisionException or IllegalArgumentException if [start..end] range is not a subrange of [0..lastRevision]
tikhomirov@347: public static void checkRevlogRange(int start, int end, int lastRevision) throws HgInvalidRevisionException {
tikhomirov@300: if (start < 0 || start > lastRevision) {
tikhomirov@347: final String m = String.format("Bad left range boundary %d in [0..%d]", start, lastRevision);
tikhomirov@347: throw new HgInvalidRevisionException(m, null, start).setRevisionIndex(start, 0, lastRevision);
tikhomirov@300: }
tikhomirov@300: if (end < 0 || end > lastRevision) {
tikhomirov@347: final String m = String.format("Bad right range boundary %d in [0..%d]", end, lastRevision);
tikhomirov@347: throw new HgInvalidRevisionException(m, null, end).setRevisionIndex(end, 0, lastRevision);
tikhomirov@300: }
tikhomirov@300: if (end < start) {
tikhomirov@300: throw new IllegalArgumentException(String.format("Bad range [%d..%d]", start, end));
tikhomirov@300: }
tikhomirov@300: }
tikhomirov@59: }