comparison hg4j/src/main/java/org/tmatesoft/hg/repo/HgLookup.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.repo;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23
24 import org.tmatesoft.hg.core.HgBadArgumentException;
25 import org.tmatesoft.hg.core.HgException;
26 import org.tmatesoft.hg.internal.ConfigFile;
27 import org.tmatesoft.hg.internal.DataAccessProvider;
28 import org.tmatesoft.hg.internal.Internals;
29
30 /**
31 * Utility methods to find Mercurial repository at a given location
32 *
33 * @author Artem Tikhomirov
34 * @author TMate Software Ltd.
35 */
36 public class HgLookup {
37
38 private ConfigFile globalCfg;
39
40 public HgRepository detectFromWorkingDir() throws HgException {
41 return detect(System.getProperty("user.dir"));
42 }
43
44 public HgRepository detect(String location) throws HgException {
45 return detect(new File(location));
46 }
47
48 // look up in specified location and above
49 public HgRepository detect(File location) throws HgException {
50 File dir = location.getAbsoluteFile();
51 File repository;
52 do {
53 repository = new File(dir, ".hg");
54 if (repository.exists() && repository.isDirectory()) {
55 break;
56 }
57 repository = null;
58 dir = dir.getParentFile();
59
60 } while(dir != null);
61 if (repository == null) {
62 // return invalid repository
63 return new HgRepository(location.getPath());
64 }
65 try {
66 String repoPath = repository.getParentFile().getCanonicalPath();
67 return new HgRepository(repoPath, repository);
68 } catch (IOException ex) {
69 throw new HgException(location.toString(), ex);
70 }
71 }
72
73 public HgBundle loadBundle(File location) throws HgException {
74 if (location == null || !location.canRead()) {
75 throw new IllegalArgumentException();
76 }
77 return new HgBundle(new DataAccessProvider(), location).link();
78 }
79
80 /**
81 * Try to instantiate remote server.
82 * @param key either URL or a key from configuration file that points to remote server
83 * @param hgRepo <em>NOT USED YET<em> local repository that may have extra config, or default remote location
84 * @return an instance featuring access to remote repository, check {@link HgRemoteRepository#isInvalid()} before actually using it
85 * @throws HgBadArgumentException if anything is wrong with the remote server's URL
86 */
87 public HgRemoteRepository detectRemote(String key, HgRepository hgRepo) throws HgBadArgumentException {
88 URL url;
89 Exception toReport;
90 try {
91 url = new URL(key);
92 toReport = null;
93 } catch (MalformedURLException ex) {
94 url = null;
95 toReport = ex;
96 }
97 if (url == null) {
98 String server = getGlobalConfig().getSection("paths").get(key);
99 if (server == null) {
100 throw new HgBadArgumentException(String.format("Can't find server %s specification in the config", key), toReport);
101 }
102 try {
103 url = new URL(server);
104 } catch (MalformedURLException ex) {
105 throw new HgBadArgumentException(String.format("Found %s server spec in the config, but failed to initialize with it", key), ex);
106 }
107 }
108 return new HgRemoteRepository(url);
109 }
110
111 public HgRemoteRepository detect(URL url) throws HgException {
112 if (url == null) {
113 throw new IllegalArgumentException();
114 }
115 if (Boolean.FALSE.booleanValue()) {
116 throw HgRepository.notImplemented();
117 }
118 return new HgRemoteRepository(url);
119 }
120
121 private ConfigFile getGlobalConfig() {
122 if (globalCfg == null) {
123 globalCfg = new Internals().newConfigFile();
124 globalCfg.addLocation(new File(System.getProperty("user.home"), ".hgrc"));
125 }
126 return globalCfg;
127 }
128 }