changeset 179:da426c2fe1ec

Support for changegroup wire command
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 06 Apr 2011 02:50:26 +0200
parents 62665d8f0686
children 42fe9a94b9d0
files cmdline/org/tmatesoft/hg/console/Remote.java src/org/tmatesoft/hg/repo/HgRemoteRepository.java
diffstat 2 files changed, 58 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/cmdline/org/tmatesoft/hg/console/Remote.java	Wed Apr 06 01:34:16 2011 +0200
+++ b/cmdline/org/tmatesoft/hg/console/Remote.java	Wed Apr 06 02:50:26 2011 +0200
@@ -95,8 +95,8 @@
 		cfg.addLocation(new File(System.getProperty("user.home"), ".hgrc"));
 		String svnkitServer = cfg.getSection("paths").get("svnkit");
 //		URL url = new URL(svnkitServer + "?cmd=branches&nodes=30bd389788464287cee22ccff54c330a4b715de5");
-		URL url = new URL(svnkitServer + "?cmd=between"); 
-//		URL url = new URL(svnkitServer + "?cmd=changegroup&roots=" + Nodeid.NULL.toString());
+//		URL url = new URL(svnkitServer + "?cmd=between"); 
+		URL url = new URL(svnkitServer + "?cmd=changegroup&roots=71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d");
 //		URL url = new URL("http://localhost:8000/" + "?cmd=between");
 //		URL url = new URL(svnkitServer + "?cmd=stream_out");
 	
@@ -125,17 +125,17 @@
 		urlConnection.setRequestProperty("Accept", "application/mercurial-0.1");
 		urlConnection.setRequestProperty("Authorization", "Basic " + authInfo);
 		urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
-		byte[] body = "pairs=f5aed108754e817d2ca374d1a4f6daf1218dcc91-9429c7bd1920fab164a9d2b621d38d57bcb49ae0".getBytes();
-		urlConnection.setRequestMethod("POST");
-		urlConnection.setRequestProperty("Content-Length", String.valueOf(body.length));
-		urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
-		urlConnection.setDoOutput(true);
-		urlConnection.setDoInput(true);
-//		urlConnection.connect();
-		OutputStream os = urlConnection.getOutputStream();
-		os.write(body);
-		os.flush();
-		os.close();
+//		byte[] body = "pairs=f5aed108754e817d2ca374d1a4f6daf1218dcc91-9429c7bd1920fab164a9d2b621d38d57bcb49ae0".getBytes();
+//		urlConnection.setRequestMethod("POST");
+//		urlConnection.setRequestProperty("Content-Length", String.valueOf(body.length));
+//		urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+//		urlConnection.setDoOutput(true);
+//		urlConnection.setDoInput(true);
+		urlConnection.connect();
+//		OutputStream os = urlConnection.getOutputStream();
+//		os.write(body);
+//		os.flush();
+//		os.close();
 		System.out.println("Query:" + url.getQuery());
 		System.out.println("Response headers:");
 		final Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
@@ -145,8 +145,8 @@
 		System.out.printf("Content type is %s and its length is %d\n", urlConnection.getContentType(), urlConnection.getContentLength());
 		InputStream is = urlConnection.getInputStream();
 		//
-		dump(is, -1); // simple dump, any cmd
-//		writeBundle(is, false, "HG10GZ"); // cmd=changegroup
+//		dump(is, -1); // simple dump, any cmd
+		writeBundle(is, false, "HG10GZ"); // cmd=changegroup
 		//writeBundle(is, true, "" or "HG10UN");
 		//
 		urlConnection.disconnect();
--- a/src/org/tmatesoft/hg/repo/HgRemoteRepository.java	Wed Apr 06 01:34:16 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRemoteRepository.java	Wed Apr 06 02:50:26 2011 +0200
@@ -17,7 +17,9 @@
 package org.tmatesoft.hg.repo;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.StreamTokenizer;
@@ -37,6 +39,7 @@
 import java.util.Map;
 import java.util.prefs.BackingStoreException;
 import java.util.prefs.Preferences;
+import java.util.zip.InflaterInputStream;
 
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLContext;
@@ -277,7 +280,30 @@
 
 	// WireProtocol wiki: roots = a list of the latest nodes on every service side changeset branch that both the client and server know about. 
 	public HgBundle getChanges(List<Nodeid> roots) throws HgException {
-		return new HgLookup().loadBundle(new File("/temp/hg/hg-bundle-000000000000-gz.tmp"));
+		StringBuilder sb = new StringBuilder(20 + roots.size() * 41);
+		sb.append("roots=");
+		for (Nodeid n : roots) {
+			sb.append(n.toString());
+			sb.append('+');
+		}
+		if (sb.charAt(sb.length() - 1) == '+') {
+			// strip last space 
+			sb.setLength(sb.length() - 1);
+		}
+		try {
+			URL u = new URL(url, url.getPath() + "?cmd=changegroup&" + sb.toString());
+			HttpURLConnection c = setupConnection(u.openConnection());
+			c.connect();
+			if (debug) {
+				dumpResponseHeader(u, c);
+			}
+			File tf = writeBundle(c.getInputStream(), false, "HG10GZ" /*didn't see any other that zip*/);
+			return new HgLookup().loadBundle(tf);
+		} catch (MalformedURLException ex) {
+			throw new HgException(ex);
+		} catch (IOException ex) {
+			throw new HgException(ex);
+		}
 	}
 	
 	private HttpURLConnection setupConnection(URLConnection urlConnection) {
@@ -300,6 +326,22 @@
 			System.out.printf("%s: %s\n", s, c.getHeaderField(s));
 		}
 	}
+	
+	private static File writeBundle(InputStream is, boolean decompress, String header) throws IOException {
+		InputStream zipStream = decompress ? new InflaterInputStream(is) : is;
+		File tf = File.createTempFile("hg-bundle-", null);
+		FileOutputStream fos = new FileOutputStream(tf);
+		fos.write(header.getBytes());
+		int r;
+		byte[] buf = new byte[8*1024];
+		while ((r = zipStream.read(buf)) != -1) {
+			fos.write(buf, 0, r);
+		}
+		fos.close();
+		zipStream.close();
+		return tf;
+	}
+
 
 	public static final class Range {
 		/**