comparison src/org/tmatesoft/hg/repo/HgLookup.java @ 181:cd3371670f0b

Refactor incoming and outgoing code to be shared with RepositoryComparator. Placeholders for in/out commands. Refactor common remote lookup code
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 12 Apr 2011 19:10:38 +0200
parents 87f40938c9b2
children 44a34baabea0
comparison
equal deleted inserted replaced
180:42fe9a94b9d0 181:cd3371670f0b
16 */ 16 */
17 package org.tmatesoft.hg.repo; 17 package org.tmatesoft.hg.repo;
18 18
19 import java.io.File; 19 import java.io.File;
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.net.MalformedURLException;
21 import java.net.URL; 22 import java.net.URL;
22 23
24 import org.tmatesoft.hg.core.HgBadArgumentException;
23 import org.tmatesoft.hg.core.HgException; 25 import org.tmatesoft.hg.core.HgException;
26 import org.tmatesoft.hg.internal.ConfigFile;
24 import org.tmatesoft.hg.internal.DataAccessProvider; 27 import org.tmatesoft.hg.internal.DataAccessProvider;
28 import org.tmatesoft.hg.internal.Internals;
25 29
26 /** 30 /**
27 * Utility methods to find Mercurial repository at a given location 31 * Utility methods to find Mercurial repository at a given location
28 * 32 *
29 * @author Artem Tikhomirov 33 * @author Artem Tikhomirov
30 * @author TMate Software Ltd. 34 * @author TMate Software Ltd.
31 */ 35 */
32 public class HgLookup { 36 public class HgLookup {
37
38 private ConfigFile globalCfg;
33 39
34 public HgRepository detectFromWorkingDir() throws HgException { 40 public HgRepository detectFromWorkingDir() throws HgException {
35 return detect(System.getProperty("user.dir")); 41 return detect(System.getProperty("user.dir"));
36 } 42 }
37 43
68 if (location == null || !location.canRead()) { 74 if (location == null || !location.canRead()) {
69 throw new IllegalArgumentException(); 75 throw new IllegalArgumentException();
70 } 76 }
71 return new HgBundle(new DataAccessProvider(), location); 77 return new HgBundle(new DataAccessProvider(), location);
72 } 78 }
73 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
74 public HgRemoteRepository detect(URL url) throws HgException { 111 public HgRemoteRepository detect(URL url) throws HgException {
75 if (url == null) { 112 if (url == null) {
76 throw new IllegalArgumentException(); 113 throw new IllegalArgumentException();
77 } 114 }
78 if (Boolean.FALSE.booleanValue()) { 115 if (Boolean.FALSE.booleanValue()) {
79 throw HgRepository.notImplemented(); 116 throw HgRepository.notImplemented();
80 } 117 }
81 return new HgRemoteRepository(url); 118 return new HgRemoteRepository(url);
82 } 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 }
83 } 128 }