comparison src/org/tmatesoft/hg/repo/HgLookup.java @ 503:0bd2d0441d8f

Add lookup of remote paths from repository's .hg/hgrc, respect default (origin)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 31 Oct 2012 15:17:21 +0100
parents e4eaa23e3442
children 2f9ed6bcefa2
comparison
equal deleted inserted replaced
502:37d97854c6ad 503:0bd2d0441d8f
26 import org.tmatesoft.hg.core.HgRepositoryNotFoundException; 26 import org.tmatesoft.hg.core.HgRepositoryNotFoundException;
27 import org.tmatesoft.hg.core.SessionContext; 27 import org.tmatesoft.hg.core.SessionContext;
28 import org.tmatesoft.hg.internal.BasicSessionContext; 28 import org.tmatesoft.hg.internal.BasicSessionContext;
29 import org.tmatesoft.hg.internal.ConfigFile; 29 import org.tmatesoft.hg.internal.ConfigFile;
30 import org.tmatesoft.hg.internal.DataAccessProvider; 30 import org.tmatesoft.hg.internal.DataAccessProvider;
31 import org.tmatesoft.hg.repo.HgRepoConfig.PathsSection;
31 32
32 /** 33 /**
33 * Utility methods to find Mercurial repository at a given location 34 * Utility methods to find Mercurial repository at a given location
34 * 35 *
35 * @author Artem Tikhomirov 36 * @author Artem Tikhomirov
81 } 82 }
82 return new HgBundle(getContext(), new DataAccessProvider(getContext()), location).link(); 83 return new HgBundle(getContext(), new DataAccessProvider(getContext()), location).link();
83 } 84 }
84 85
85 /** 86 /**
86 * Try to instantiate remote server. 87 * Try to instantiate remote server using an immediate url or an url from configuration files
87 * @param key either URL or a key from configuration file that points to remote server 88 *
88 * @param hgRepo <em>NOT USED YET<em> local repository that may have extra config, or default remote location 89 * @param key either URL or a key from configuration file that points to remote server; if <code>null</code> or empty string, default remote location of the supplied repository (if any) is looked up
90 * @param hgRepo optional local repository to get default or otherwise configured remote location
89 * @return an instance featuring access to remote repository, check {@link HgRemoteRepository#isInvalid()} before actually using it 91 * @return an instance featuring access to remote repository, check {@link HgRemoteRepository#isInvalid()} before actually using it
90 * @throws HgBadArgumentException if anything is wrong with the remote server's URL 92 * @throws HgBadArgumentException if anything is wrong with the remote server's URL
91 */ 93 */
92 public HgRemoteRepository detectRemote(String key, HgRepository hgRepo) throws HgBadArgumentException { 94 public HgRemoteRepository detectRemote(String key, HgRepository hgRepo) throws HgBadArgumentException {
93 URL url; 95 URL url;
98 } catch (MalformedURLException ex) { 100 } catch (MalformedURLException ex) {
99 url = null; 101 url = null;
100 toReport = ex; 102 toReport = ex;
101 } 103 }
102 if (url == null) { 104 if (url == null) {
103 String server = getGlobalConfig().getSection("paths").get(key); 105 String server = null;
106 if (hgRepo != null && !hgRepo.isInvalid()) {
107 PathsSection ps = hgRepo.getConfiguration().getPaths();
108 server = key == null || key.trim().isEmpty() ? ps.getDefault() : ps.getString(key, null);
109 } else if (key == null || key.trim().length() == 0) {
110 throw new HgBadArgumentException("Can't look up empty key in a global configuration", null);
111 }
112 if (server == null) {
113 server = getGlobalConfig().getSection("paths").get(key);
114 }
104 if (server == null) { 115 if (server == null) {
105 throw new HgBadArgumentException(String.format("Can't find server %s specification in the config", key), toReport); 116 throw new HgBadArgumentException(String.format("Can't find server %s specification in the config", key), toReport);
106 } 117 }
107 try { 118 try {
108 url = new URL(server); 119 url = new URL(server);