comparison src/org/tmatesoft/hg/repo/HgRemoteRepository.java @ 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 cd3371670f0b
comparison
equal deleted inserted replaced
178:62665d8f0686 179:da426c2fe1ec
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
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.FileOutputStream;
20 import java.io.IOException; 21 import java.io.IOException;
22 import java.io.InputStream;
21 import java.io.InputStreamReader; 23 import java.io.InputStreamReader;
22 import java.io.OutputStream; 24 import java.io.OutputStream;
23 import java.io.StreamTokenizer; 25 import java.io.StreamTokenizer;
24 import java.net.HttpURLConnection; 26 import java.net.HttpURLConnection;
25 import java.net.MalformedURLException; 27 import java.net.MalformedURLException;
35 import java.util.LinkedList; 37 import java.util.LinkedList;
36 import java.util.List; 38 import java.util.List;
37 import java.util.Map; 39 import java.util.Map;
38 import java.util.prefs.BackingStoreException; 40 import java.util.prefs.BackingStoreException;
39 import java.util.prefs.Preferences; 41 import java.util.prefs.Preferences;
42 import java.util.zip.InflaterInputStream;
40 43
41 import javax.net.ssl.HttpsURLConnection; 44 import javax.net.ssl.HttpsURLConnection;
42 import javax.net.ssl.SSLContext; 45 import javax.net.ssl.SSLContext;
43 import javax.net.ssl.TrustManager; 46 import javax.net.ssl.TrustManager;
44 import javax.net.ssl.X509TrustManager; 47 import javax.net.ssl.X509TrustManager;
275 } 278 }
276 } 279 }
277 280
278 // WireProtocol wiki: roots = a list of the latest nodes on every service side changeset branch that both the client and server know about. 281 // WireProtocol wiki: roots = a list of the latest nodes on every service side changeset branch that both the client and server know about.
279 public HgBundle getChanges(List<Nodeid> roots) throws HgException { 282 public HgBundle getChanges(List<Nodeid> roots) throws HgException {
280 return new HgLookup().loadBundle(new File("/temp/hg/hg-bundle-000000000000-gz.tmp")); 283 StringBuilder sb = new StringBuilder(20 + roots.size() * 41);
284 sb.append("roots=");
285 for (Nodeid n : roots) {
286 sb.append(n.toString());
287 sb.append('+');
288 }
289 if (sb.charAt(sb.length() - 1) == '+') {
290 // strip last space
291 sb.setLength(sb.length() - 1);
292 }
293 try {
294 URL u = new URL(url, url.getPath() + "?cmd=changegroup&" + sb.toString());
295 HttpURLConnection c = setupConnection(u.openConnection());
296 c.connect();
297 if (debug) {
298 dumpResponseHeader(u, c);
299 }
300 File tf = writeBundle(c.getInputStream(), false, "HG10GZ" /*didn't see any other that zip*/);
301 return new HgLookup().loadBundle(tf);
302 } catch (MalformedURLException ex) {
303 throw new HgException(ex);
304 } catch (IOException ex) {
305 throw new HgException(ex);
306 }
281 } 307 }
282 308
283 private HttpURLConnection setupConnection(URLConnection urlConnection) { 309 private HttpURLConnection setupConnection(URLConnection urlConnection) {
284 urlConnection.setRequestProperty("User-Agent", "hg4j/0.5.0"); 310 urlConnection.setRequestProperty("User-Agent", "hg4j/0.5.0");
285 urlConnection.addRequestProperty("Accept", "application/mercurial-0.1"); 311 urlConnection.addRequestProperty("Accept", "application/mercurial-0.1");
298 final Map<String, List<String>> headerFields = c.getHeaderFields(); 324 final Map<String, List<String>> headerFields = c.getHeaderFields();
299 for (String s : headerFields.keySet()) { 325 for (String s : headerFields.keySet()) {
300 System.out.printf("%s: %s\n", s, c.getHeaderField(s)); 326 System.out.printf("%s: %s\n", s, c.getHeaderField(s));
301 } 327 }
302 } 328 }
329
330 private static File writeBundle(InputStream is, boolean decompress, String header) throws IOException {
331 InputStream zipStream = decompress ? new InflaterInputStream(is) : is;
332 File tf = File.createTempFile("hg-bundle-", null);
333 FileOutputStream fos = new FileOutputStream(tf);
334 fos.write(header.getBytes());
335 int r;
336 byte[] buf = new byte[8*1024];
337 while ((r = zipStream.read(buf)) != -1) {
338 fos.write(buf, 0, r);
339 }
340 fos.close();
341 zipStream.close();
342 return tf;
343 }
344
303 345
304 public static final class Range { 346 public static final class Range {
305 /** 347 /**
306 * Root of the range, earlier revision 348 * Root of the range, earlier revision
307 */ 349 */