tikhomirov@74: /* 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@2: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@2: tikhomirov@2: import java.io.File; tikhomirov@148: import java.io.IOException; tikhomirov@148: tikhomirov@148: import org.tmatesoft.hg.core.HgException; tikhomirov@2: tikhomirov@2: /** tikhomirov@148: * Utility methods to find Mercurial repository at a given location tikhomirov@74: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@2: */ tikhomirov@95: public class HgLookup { tikhomirov@2: tikhomirov@148: public HgRepository detectFromWorkingDir() throws HgException { tikhomirov@2: return detect(System.getProperty("user.dir")); tikhomirov@2: } tikhomirov@2: tikhomirov@148: public HgRepository detect(String location) throws HgException { tikhomirov@95: return detect(new File(location)); tikhomirov@95: } tikhomirov@95: tikhomirov@95: // look up in specified location and above tikhomirov@148: public HgRepository detect(File location) throws HgException { tikhomirov@95: File dir = location; tikhomirov@2: File repository; tikhomirov@2: do { tikhomirov@2: repository = new File(dir, ".hg"); tikhomirov@2: if (repository.exists() && repository.isDirectory()) { tikhomirov@2: break; tikhomirov@2: } tikhomirov@2: repository = null; tikhomirov@2: dir = dir.getParentFile(); tikhomirov@2: tikhomirov@2: } while(dir != null); tikhomirov@2: if (repository == null) { tikhomirov@95: // return invalid repository tikhomirov@95: return new HgRepository(location.getPath()); tikhomirov@2: } tikhomirov@148: try { tikhomirov@148: String repoPath = repository.getParentFile().getCanonicalPath(); tikhomirov@148: return new HgRepository(repoPath, repository); tikhomirov@148: } catch (IOException ex) { tikhomirov@148: throw new HgException(location.toString(), ex); tikhomirov@148: } tikhomirov@4: } tikhomirov@2: }