diff src/org/tmatesoft/hg/repo/HgLookup.java @ 171:2c3e96674e2a

Towards outgoing changes - initial detection logic, get connected with remote repo stub
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 25 Mar 2011 00:05:52 +0100
parents 71ddbf8603e8
children 87f40938c9b2
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/repo/HgLookup.java	Wed Mar 23 20:46:00 2011 +0100
+++ b/src/org/tmatesoft/hg/repo/HgLookup.java	Fri Mar 25 00:05:52 2011 +0100
@@ -18,10 +18,13 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
 
 import org.tmatesoft.hg.core.HgException;
+import org.tmatesoft.hg.internal.ConfigFile;
 import org.tmatesoft.hg.internal.DataAccessProvider;
+import org.tmatesoft.hg.internal.Internals;
 
 /**
  * Utility methods to find Mercurial repository at a given location
@@ -72,9 +75,27 @@
 	}
 
 	public HgRemoteRepository detect(URL url) throws HgException {
+		if (url == null) {
+			throw new IllegalArgumentException();
+		}
 		if (Boolean.FALSE.booleanValue()) {
 			throw HgRepository.notImplemented();
 		}
-		return null;
+		if (url.getProtocol() == null) {
+			// try configuration keys
+			String key = url.getHost();
+			ConfigFile cfg = new Internals().newConfigFile();
+			cfg.addLocation(new File(System.getProperty("user.home"), ".hgrc"));
+			String server = cfg.getSection("paths").get(key);
+			if (server == null) {
+				throw new HgException(String.format("Can't find server %s specification in the config", key));
+			}
+			try {
+				url = new URL(server);
+			} catch (MalformedURLException ex) {
+				throw new HgException(ex);
+			}
+		}
+		return new HgRemoteRepository(url);
 	}
 }