kitaev@213: /* kitaev@213: * Copyright (c) 2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.console; kitaev@213: kitaev@213: import java.io.File; kitaev@213: import java.io.FileOutputStream; kitaev@213: import java.io.IOException; kitaev@213: import java.io.InputStream; kitaev@213: import java.io.OutputStream; kitaev@213: import java.net.HttpURLConnection; kitaev@213: import java.net.URL; kitaev@213: import java.security.cert.CertificateException; kitaev@213: import java.security.cert.X509Certificate; kitaev@213: import java.util.List; kitaev@213: import java.util.Map; kitaev@213: import java.util.prefs.Preferences; kitaev@213: import java.util.zip.InflaterInputStream; kitaev@213: kitaev@213: import javax.net.ssl.HttpsURLConnection; kitaev@213: import javax.net.ssl.SSLContext; kitaev@213: import javax.net.ssl.TrustManager; kitaev@213: import javax.net.ssl.X509TrustManager; kitaev@213: kitaev@213: import org.tmatesoft.hg.internal.ConfigFile; kitaev@213: import org.tmatesoft.hg.internal.Internals; kitaev@213: kitaev@213: /** kitaev@213: * WORK IN PROGRESS, DO NOT USE kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class Remote { kitaev@213: kitaev@213: /* kitaev@213: * @see http://mercurial.selenic.com/wiki/WireProtocol kitaev@213: cmd=branches gives 4 nodeids (head, root, first parent, second parent) per line (few lines possible, per branch, perhaps?) kitaev@213: cmd=capabilities gives lookup ...subset and 3 compress methods kitaev@213: // lookup changegroupsubset unbundle=HG10GZ,HG10BZ,HG10UN kitaev@213: cmd=heads gives space-separated list of nodeids (or just one) kitaev@213: nodeids are in hex (printable) format, need to convert fromAscii() kitaev@213: cmd=branchmap kitaev@213: cmd=between needs argument pairs, with first element in the pair to be head(!), second to be root of the branch ( kitaev@213: i.e. (newer-older), not (older-newer) as one might expect. Returned list of nodes comes in reversed order (from newer kitaev@213: to older) as well kitaev@213: kitaev@213: cmd=branches&nodes=d6d2a630f4a6d670c90a5ca909150f2b426ec88f+ kitaev@213: head, root, first parent, second parent kitaev@213: received: d6d2a630f4a6d670c90a5ca909150f2b426ec88f dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 kitaev@213: kitaev@213: Sequence, for actual state with merged/closed branch, where 157:d5268ca7715b8d96204fc62abc632e8f55761547 is merge revision of 156 and 53 kitaev@213: >branches, 170:71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d kitaev@213: 71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d d5268ca7715b8d96204fc62abc632e8f55761547 643ddec3be36246fc052cf22ece503fa60cafe22 a6f39e595b2b54f56304470269a936ead77f5725 kitaev@213: kitaev@213: >branches, 156:643ddec3be36246fc052cf22ece503fa60cafe22 kitaev@213: 643ddec3be36246fc052cf22ece503fa60cafe22 ade65afe0906febafbf8a2e41002052e0e446471 08754fce5778a3409476ecdb3cec6b5172c34367 40d04c4f771ebbd599eb229145252732a596740a kitaev@213: >branches, 53:a6f39e595b2b54f56304470269a936ead77f5725 kitaev@213: a6f39e595b2b54f56304470269a936ead77f5725 a6f39e595b2b54f56304470269a936ead77f5725 9429c7bd1920fab164a9d2b621d38d57bcb49ae0 30bd389788464287cee22ccff54c330a4b715de5 kitaev@213: kitaev@213: >branches, 84:08754fce5778a3409476ecdb3cec6b5172c34367 (p1:82) kitaev@213: 08754fce5778a3409476ecdb3cec6b5172c34367 dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 kitaev@213: >branches, 83:40d04c4f771ebbd599eb229145252732a596740a (p1:80) kitaev@213: 40d04c4f771ebbd599eb229145252732a596740a dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 kitaev@213: kitaev@213: >branches, 51:9429c7bd1920fab164a9d2b621d38d57bcb49ae0 (wrap-data-access branch) kitaev@213: 9429c7bd1920fab164a9d2b621d38d57bcb49ae0 dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 kitaev@213: >branches, 52:30bd389788464287cee22ccff54c330a4b715de5 (p1:50) kitaev@213: 30bd389788464287cee22ccff54c330a4b715de5 dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 kitaev@213: kitaev@213: kitaev@213: cmd=between&pairs=71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d-d5268ca7715b8d96204fc62abc632e8f55761547+40d04c4f771ebbd599eb229145252732a596740a-dbd663faec1f0175619cf7668bddc6350548b8d6 kitaev@213: 8c8e3f372fa1fbfcf92b004b6f2ada2dbaf60028 dd525ca65de8e78cb133919de57ea0a6e6454664 1d0654be1466d522994f8bead510e360fbeb8d79 c17a08095e4420202ac1b2d939ef6d5f8bebb569 kitaev@213: 4222b04f34ee885bc1ad547c7ef330e18a51afc1 5f9635c016819b322ae05a91b3378621b538c933 c677e159391925a50b9a23f557426b2246bc9c5d 0d279bcc44427cb5ae2f3407c02f21187ccc8aea e21df6259f8374ac136767321e837c0c6dd21907 b01500fe2604c2c7eadf44349cce9f438484474b 865bf07f381ff7d1b742453568def92576af80b6 kitaev@213: kitaev@213: Between two subsequent revisions (i.e. direct child in remote of a local root) kitaev@213: cmd=between&pairs=71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d-8c8e3f372fa1fbfcf92b004b6f2ada2dbaf60028 kitaev@213: empty result kitaev@213: */ kitaev@213: public static void main(String[] args) throws Exception { kitaev@213: ConfigFile cfg = new Internals().newConfigFile(); kitaev@213: cfg.addLocation(new File(System.getProperty("user.home"), ".hgrc")); kitaev@213: String svnkitServer = cfg.getSection("paths").get("svnkit"); kitaev@213: // URL url = new URL(svnkitServer + "?cmd=branches&nodes=30bd389788464287cee22ccff54c330a4b715de5"); kitaev@213: // URL url = new URL(svnkitServer + "?cmd=between"); kitaev@213: URL url = new URL(svnkitServer + "?cmd=changegroup&roots=71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d"); kitaev@213: // URL url = new URL("http://localhost:8000/" + "?cmd=between"); kitaev@213: // URL url = new URL(svnkitServer + "?cmd=stream_out"); kitaev@213: kitaev@213: SSLContext sslContext = SSLContext.getInstance("SSL"); kitaev@213: class TrustEveryone implements X509TrustManager { kitaev@213: public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { kitaev@213: System.out.println("checkClientTrusted " + authType); kitaev@213: } kitaev@213: public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { kitaev@213: System.out.println("checkServerTrusted" + authType); kitaev@213: } kitaev@213: public X509Certificate[] getAcceptedIssuers() { kitaev@213: return new X509Certificate[0]; kitaev@213: } kitaev@213: } kitaev@213: // Hack to get Base64-encoded credentials kitaev@213: Preferences tempNode = Preferences.userRoot().node("xxx"); kitaev@213: tempNode.putByteArray("xxx", url.getUserInfo().getBytes()); kitaev@213: String authInfo = tempNode.get("xxx", null); kitaev@213: tempNode.removeNode(); kitaev@213: // kitaev@213: sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null); kitaev@213: HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); kitaev@213: // HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); kitaev@213: urlConnection.setRequestProperty("User-Agent", "jhg/0.1.0"); kitaev@213: urlConnection.setRequestProperty("Accept", "application/mercurial-0.1"); kitaev@213: urlConnection.setRequestProperty("Authorization", "Basic " + authInfo); kitaev@213: urlConnection.setSSLSocketFactory(sslContext.getSocketFactory()); kitaev@213: // byte[] body = "pairs=f5aed108754e817d2ca374d1a4f6daf1218dcc91-9429c7bd1920fab164a9d2b621d38d57bcb49ae0".getBytes(); kitaev@213: // urlConnection.setRequestMethod("POST"); kitaev@213: // urlConnection.setRequestProperty("Content-Length", String.valueOf(body.length)); kitaev@213: // urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); kitaev@213: // urlConnection.setDoOutput(true); kitaev@213: // urlConnection.setDoInput(true); kitaev@213: urlConnection.connect(); kitaev@213: // OutputStream os = urlConnection.getOutputStream(); kitaev@213: // os.write(body); kitaev@213: // os.flush(); kitaev@213: // os.close(); kitaev@213: System.out.println("Query:" + url.getQuery()); kitaev@213: System.out.println("Response headers:"); kitaev@213: final Map> headerFields = urlConnection.getHeaderFields(); kitaev@213: for (String s : headerFields.keySet()) { kitaev@213: System.out.printf("%s: %s\n", s, urlConnection.getHeaderField(s)); kitaev@213: } kitaev@213: System.out.printf("Content type is %s and its length is %d\n", urlConnection.getContentType(), urlConnection.getContentLength()); kitaev@213: InputStream is = urlConnection.getInputStream(); kitaev@213: // kitaev@213: // dump(is, -1); // simple dump, any cmd kitaev@213: writeBundle(is, false, "HG10GZ"); // cmd=changegroup kitaev@213: //writeBundle(is, true, "" or "HG10UN"); kitaev@213: // kitaev@213: urlConnection.disconnect(); kitaev@213: // kitaev@213: } kitaev@213: kitaev@213: private static void dump(InputStream is, int limit) throws IOException { kitaev@213: int b; kitaev@213: while ((b =is.read()) != -1) { kitaev@213: System.out.print((char) b); kitaev@213: if (limit != -1) { kitaev@213: if (--limit < 0) { kitaev@213: break; kitaev@213: } kitaev@213: } kitaev@213: } kitaev@213: System.out.println(); kitaev@213: } kitaev@213: kitaev@213: private static void writeBundle(InputStream is, boolean decompress, String header) throws IOException { kitaev@213: InputStream zipStream = decompress ? new InflaterInputStream(is) : is; kitaev@213: File tf = File.createTempFile("hg-bundle-", null); kitaev@213: FileOutputStream fos = new FileOutputStream(tf); kitaev@213: fos.write(header.getBytes()); kitaev@213: int r; kitaev@213: byte[] buf = new byte[8*1024]; kitaev@213: while ((r = zipStream.read(buf)) != -1) { kitaev@213: fos.write(buf, 0, r); kitaev@213: } kitaev@213: fos.close(); kitaev@213: zipStream.close(); kitaev@213: System.out.println(tf); kitaev@213: } kitaev@213: }