tikhomirov@59: /* 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@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@347: import org.tmatesoft.hg.core.HgInvalidRevisionException; tikhomirov@295: import org.tmatesoft.hg.core.SessionContext; tikhomirov@229: import org.tmatesoft.hg.internal.Experimental; tikhomirov@229: import org.tmatesoft.hg.internal.RelativePathRewrite; 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@284: import org.tmatesoft.hg.util.PathPool; 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@229: * 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@59: public void dumpDirstate() { tikhomirov@296: getDirstate().dump(); tikhomirov@296: } tikhomirov@296: tikhomirov@296: public HgDirstate getDirstate() { tikhomirov@296: return repo.loadDirstate(new PathPool(new PathRewrite.Empty())); tikhomirov@296: } tikhomirov@296: tikhomirov@296: // tests tikhomirov@296: public HgDirstate createDirstate(boolean caseSensitiveFileSystem) { 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@296: return new HgDirstate(repo, new File(repo.getRepositoryRoot(), "dirstate"), new PathPool(new PathRewrite.Empty()), canonicalPath); 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@59: tikhomirov@114: public File getRepositoryDir() { tikhomirov@114: return repo.getRepositoryRoot(); tikhomirov@114: } tikhomirov@114: tikhomirov@269: public static HgIgnore newHgIgnore(Reader source) throws IOException { tikhomirov@269: HgIgnore hgIgnore = new HgIgnore(); 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: @Experimental(reason="Don't want to expose io.File from HgRepository; need to create FileIterator for working dir. Need a place to keep that code") 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@229: return new FileWalker(repoRoot, pathSrc, workindDirScope); tikhomirov@229: } tikhomirov@295: tikhomirov@295: // expose othewise package-local information primarily to use in our own o.t.hg.core package tikhomirov@295: public static SessionContext getContext(HgRepository repo) { tikhomirov@295: return repo.getContext(); tikhomirov@295: } tikhomirov@229: tikhomirov@148: tikhomirov@148: // Convenient check of local revision number for validity (not all negative values are wrong as long as we use negative constants) tikhomirov@148: public static boolean wrongLocalRevision(int rev) { tikhomirov@148: return rev < 0 && rev != TIP && rev != WORKING_COPY && rev != BAD_REVISION; tikhomirov@148: } tikhomirov@300: 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: }