comparison hg4j-cli/src/main/java/org/tmatesoft/hg/console/Remote.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.console;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.net.HttpURLConnection;
25 import java.net.URL;
26 import java.security.cert.CertificateException;
27 import java.security.cert.X509Certificate;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.prefs.Preferences;
31 import java.util.zip.InflaterInputStream;
32
33 import javax.net.ssl.HttpsURLConnection;
34 import javax.net.ssl.SSLContext;
35 import javax.net.ssl.TrustManager;
36 import javax.net.ssl.X509TrustManager;
37
38 import org.tmatesoft.hg.internal.ConfigFile;
39 import org.tmatesoft.hg.internal.Internals;
40
41 /**
42 * WORK IN PROGRESS, DO NOT USE
43 *
44 * @author Artem Tikhomirov
45 * @author TMate Software Ltd.
46 */
47 public class Remote {
48
49 /*
50 * @see http://mercurial.selenic.com/wiki/WireProtocol
51 cmd=branches gives 4 nodeids (head, root, first parent, second parent) per line (few lines possible, per branch, perhaps?)
52 cmd=capabilities gives lookup ...subset and 3 compress methods
53 // lookup changegroupsubset unbundle=HG10GZ,HG10BZ,HG10UN
54 cmd=heads gives space-separated list of nodeids (or just one)
55 nodeids are in hex (printable) format, need to convert fromAscii()
56 cmd=branchmap
57 cmd=between needs argument pairs, with first element in the pair to be head(!), second to be root of the branch (
58 i.e. (newer-older), not (older-newer) as one might expect. Returned list of nodes comes in reversed order (from newer
59 to older) as well
60
61 cmd=branches&nodes=d6d2a630f4a6d670c90a5ca909150f2b426ec88f+
62 head, root, first parent, second parent
63 received: d6d2a630f4a6d670c90a5ca909150f2b426ec88f dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000
64
65 Sequence, for actual state with merged/closed branch, where 157:d5268ca7715b8d96204fc62abc632e8f55761547 is merge revision of 156 and 53
66 >branches, 170:71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d
67 71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d d5268ca7715b8d96204fc62abc632e8f55761547 643ddec3be36246fc052cf22ece503fa60cafe22 a6f39e595b2b54f56304470269a936ead77f5725
68
69 >branches, 156:643ddec3be36246fc052cf22ece503fa60cafe22
70 643ddec3be36246fc052cf22ece503fa60cafe22 ade65afe0906febafbf8a2e41002052e0e446471 08754fce5778a3409476ecdb3cec6b5172c34367 40d04c4f771ebbd599eb229145252732a596740a
71 >branches, 53:a6f39e595b2b54f56304470269a936ead77f5725
72 a6f39e595b2b54f56304470269a936ead77f5725 a6f39e595b2b54f56304470269a936ead77f5725 9429c7bd1920fab164a9d2b621d38d57bcb49ae0 30bd389788464287cee22ccff54c330a4b715de5
73
74 >branches, 84:08754fce5778a3409476ecdb3cec6b5172c34367 (p1:82)
75 08754fce5778a3409476ecdb3cec6b5172c34367 dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000
76 >branches, 83:40d04c4f771ebbd599eb229145252732a596740a (p1:80)
77 40d04c4f771ebbd599eb229145252732a596740a dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000
78
79 >branches, 51:9429c7bd1920fab164a9d2b621d38d57bcb49ae0 (wrap-data-access branch)
80 9429c7bd1920fab164a9d2b621d38d57bcb49ae0 dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000
81 >branches, 52:30bd389788464287cee22ccff54c330a4b715de5 (p1:50)
82 30bd389788464287cee22ccff54c330a4b715de5 dbd663faec1f0175619cf7668bddc6350548b8d6 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000
83
84
85 cmd=between&pairs=71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d-d5268ca7715b8d96204fc62abc632e8f55761547+40d04c4f771ebbd599eb229145252732a596740a-dbd663faec1f0175619cf7668bddc6350548b8d6
86 8c8e3f372fa1fbfcf92b004b6f2ada2dbaf60028 dd525ca65de8e78cb133919de57ea0a6e6454664 1d0654be1466d522994f8bead510e360fbeb8d79 c17a08095e4420202ac1b2d939ef6d5f8bebb569
87 4222b04f34ee885bc1ad547c7ef330e18a51afc1 5f9635c016819b322ae05a91b3378621b538c933 c677e159391925a50b9a23f557426b2246bc9c5d 0d279bcc44427cb5ae2f3407c02f21187ccc8aea e21df6259f8374ac136767321e837c0c6dd21907 b01500fe2604c2c7eadf44349cce9f438484474b 865bf07f381ff7d1b742453568def92576af80b6
88
89 Between two subsequent revisions (i.e. direct child in remote of a local root)
90 cmd=between&pairs=71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d-8c8e3f372fa1fbfcf92b004b6f2ada2dbaf60028
91 empty result
92 */
93 public static void main(String[] args) throws Exception {
94 ConfigFile cfg = new Internals().newConfigFile();
95 cfg.addLocation(new File(System.getProperty("user.home"), ".hgrc"));
96 String svnkitServer = cfg.getSection("paths").get("svnkit");
97 // URL url = new URL(svnkitServer + "?cmd=branches&nodes=30bd389788464287cee22ccff54c330a4b715de5");
98 // URL url = new URL(svnkitServer + "?cmd=between");
99 URL url = new URL(svnkitServer + "?cmd=changegroup&roots=71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d");
100 // URL url = new URL("http://localhost:8000/" + "?cmd=between");
101 // URL url = new URL(svnkitServer + "?cmd=stream_out");
102
103 SSLContext sslContext = SSLContext.getInstance("SSL");
104 class TrustEveryone implements X509TrustManager {
105 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
106 System.out.println("checkClientTrusted " + authType);
107 }
108 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
109 System.out.println("checkServerTrusted" + authType);
110 }
111 public X509Certificate[] getAcceptedIssuers() {
112 return new X509Certificate[0];
113 }
114 }
115 // Hack to get Base64-encoded credentials
116 Preferences tempNode = Preferences.userRoot().node("xxx");
117 tempNode.putByteArray("xxx", url.getUserInfo().getBytes());
118 String authInfo = tempNode.get("xxx", null);
119 tempNode.removeNode();
120 //
121 sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null);
122 HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
123 // HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
124 urlConnection.setRequestProperty("User-Agent", "jhg/0.1.0");
125 urlConnection.setRequestProperty("Accept", "application/mercurial-0.1");
126 urlConnection.setRequestProperty("Authorization", "Basic " + authInfo);
127 urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
128 // byte[] body = "pairs=f5aed108754e817d2ca374d1a4f6daf1218dcc91-9429c7bd1920fab164a9d2b621d38d57bcb49ae0".getBytes();
129 // urlConnection.setRequestMethod("POST");
130 // urlConnection.setRequestProperty("Content-Length", String.valueOf(body.length));
131 // urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
132 // urlConnection.setDoOutput(true);
133 // urlConnection.setDoInput(true);
134 urlConnection.connect();
135 // OutputStream os = urlConnection.getOutputStream();
136 // os.write(body);
137 // os.flush();
138 // os.close();
139 System.out.println("Query:" + url.getQuery());
140 System.out.println("Response headers:");
141 final Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
142 for (String s : headerFields.keySet()) {
143 System.out.printf("%s: %s\n", s, urlConnection.getHeaderField(s));
144 }
145 System.out.printf("Content type is %s and its length is %d\n", urlConnection.getContentType(), urlConnection.getContentLength());
146 InputStream is = urlConnection.getInputStream();
147 //
148 // dump(is, -1); // simple dump, any cmd
149 writeBundle(is, false, "HG10GZ"); // cmd=changegroup
150 //writeBundle(is, true, "" or "HG10UN");
151 //
152 urlConnection.disconnect();
153 //
154 }
155
156 private static void dump(InputStream is, int limit) throws IOException {
157 int b;
158 while ((b =is.read()) != -1) {
159 System.out.print((char) b);
160 if (limit != -1) {
161 if (--limit < 0) {
162 break;
163 }
164 }
165 }
166 System.out.println();
167 }
168
169 private static void writeBundle(InputStream is, boolean decompress, String header) throws IOException {
170 InputStream zipStream = decompress ? new InflaterInputStream(is) : is;
171 File tf = File.createTempFile("hg-bundle-", null);
172 FileOutputStream fos = new FileOutputStream(tf);
173 fos.write(header.getBytes());
174 int r;
175 byte[] buf = new byte[8*1024];
176 while ((r = zipStream.read(buf)) != -1) {
177 fos.write(buf, 0, r);
178 }
179 fos.close();
180 zipStream.close();
181 System.out.println(tf);
182 }
183 }