comparison hg4j-cli/src/main/java/org/tmatesoft/hg/console/Incoming.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.util.Arrays;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map.Entry;
28
29 import org.tmatesoft.hg.core.HgIncomingCommand;
30 import org.tmatesoft.hg.core.HgRepoFacade;
31 import org.tmatesoft.hg.core.Nodeid;
32 import org.tmatesoft.hg.repo.HgLookup;
33 import org.tmatesoft.hg.repo.HgRemoteRepository;
34
35
36 /**
37 * <em>hg incoming</em> counterpart
38 *
39 * @author Artem Tikhomirov
40 * @author TMate Software Ltd.
41 */
42 public class Incoming {
43
44 public static void main(String[] args) throws Exception {
45 if (Boolean.FALSE.booleanValue()) {
46 new SequenceConstructor().test();
47 return;
48 }
49 Options cmdLineOpts = Options.parse(args);
50 HgRepoFacade hgRepo = new HgRepoFacade();
51 if (!hgRepo.init(cmdLineOpts.findRepository())) {
52 System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation());
53 return;
54 }
55 HgRemoteRepository hgRemote = new HgLookup().detectRemote(cmdLineOpts.getSingle(""), hgRepo.getRepository());
56 if (hgRemote.isInvalid()) {
57 System.err.printf("Remote repository %s is not valid", hgRemote.getLocation());
58 return;
59 }
60 HgIncomingCommand cmd = hgRepo.createIncomingCommand();
61 cmd.against(hgRemote);
62 //
63 List<Nodeid> missing = cmd.executeLite(null);
64 Collections.reverse(missing); // useful to test output, from newer to older
65 Outgoing.dump("Nodes to fetch:", missing);
66 System.out.printf("Total: %d\n\n", missing.size());
67 //
68 // Complete
69 final ChangesetDumpHandler h = new ChangesetDumpHandler(hgRepo.getRepository());
70 h.complete(false); // this option looks up index of parent revision, done via repo.changelog (which doesn't have any of these new revisions)
71 // this can be fixed by tracking all nodeid->revision idx inside ChangesetDumpHandler, and refer to repo.changelog only when that mapping didn't work
72 h.verbose(cmdLineOpts.getBoolean("-v", "--verbose"));
73 cmd.executeFull(h);
74 }
75
76
77 /*
78 * This is for investigation purposes only
79 */
80 private static class SequenceConstructor {
81
82 private int[] between(int root, int head) {
83 if (head <= (root+1)) {
84 return new int[0];
85 }
86 System.out.printf("[%d, %d]\t\t", root, head);
87 int size = 1 + (int) Math.floor(Math.log(head-root - 1) / Math.log(2));
88 int[] rv = new int[size];
89 for (int v = 1, i = 0; i < rv.length; i++) {
90 rv[i] = root + v;
91 v = v << 1;
92 }
93 System.out.println(Arrays.toString(rv));
94 return rv;
95 }
96
97 public void test() {
98 int root = 0, head = 126;
99 int[] data = between(root, head); // max number of elements to recover is 2**data.length-1, when head is exactly
100 // 2**data.length element of the branch.
101 // In such case, total number of elements in the branch (including head and root, would be 2**data.length+1
102 int[] finalSequence = new int[1 + (1 << data.length >>> 5)]; // div 32 - total bits to integers, +1 for possible modulus
103 int exactNumberOfElements = -1; // exact number of meaningful bits in finalSequence
104 LinkedHashMap<Integer, int[]> datas = new LinkedHashMap<Integer, int[]>();
105 datas.put(root, data);
106 int totalQueries = 1;
107 HashSet<Integer> queried = new HashSet<Integer>();
108 int[] checkSequence = null;
109 while(!datas.isEmpty()) {
110 LinkedList<int[]> toQuery = new LinkedList<int[]>();
111 do {
112 Iterator<Entry<Integer, int[]>> it = datas.entrySet().iterator();
113 Entry<Integer, int[]> next = it.next();
114 int r = next.getKey();
115 data = next.getValue();
116 it.remove();
117 populate(r, head, data, finalSequence);
118 if (checkSequence != null) {
119 boolean match = true;
120 // System.out.println("Try to match:");
121 for (int i = 0; i < checkSequence.length; i++) {
122 // System.out.println(i);
123 // System.out.println("control:" + toBinaryString(checkSequence[i], ' '));
124 // System.out.println("present:" + toBinaryString(finalSequence[i], ' '));
125 if (checkSequence[i] != finalSequence[i]) {
126 match = false;
127 } else {
128 match &= true;
129 }
130 }
131 System.out.println(match ? "Match, on query:" + totalQueries : "Didn't match");
132 }
133 if (data.length > 1) {
134 /*queries for elements next to head is senseless, hence data.length check above and head-x below*/
135 for (int x : data) {
136 if (!queried.contains(x) && head - x > 1) {
137 toQuery.add(new int[] {x, head});
138 }
139 }
140 }
141 } while (!datas.isEmpty()) ;
142 if (!toQuery.isEmpty()) {
143 System.out.println();
144 totalQueries++;
145 }
146 Collections.sort(toQuery, new Comparator<int[]>() {
147
148 public int compare(int[] o1, int[] o2) {
149 return o1[0] < o2[0] ? -1 : (o1[0] == o2[0] ? 0 : 1);
150 }
151 });
152 for (int[] x : toQuery) {
153 if (!queried.contains(x[0])) {
154 queried.add(x[0]);
155 data = between(x[0], x[1]);
156 if (exactNumberOfElements == -1 && data.length == 1) {
157 exactNumberOfElements = x[0] + 1;
158 System.out.printf("On query %d found out exact number of missing elements: %d\n", totalQueries, exactNumberOfElements);
159 // get a bit sequence of exactNumberOfElements, 0111..110
160 // to 'and' it with finalSequence later
161 int totalInts = (exactNumberOfElements + 2 /*heading and tailing zero bits*/) >>> 5;
162 int trailingBits = (exactNumberOfElements + 2) & 0x1f;
163 if (trailingBits != 0) {
164 totalInts++;
165 }
166 checkSequence = new int[totalInts];
167 Arrays.fill(checkSequence, 0xffffffff);
168 checkSequence[0] &= 0x7FFFFFFF;
169 if (trailingBits == 0) {
170 checkSequence[totalInts-1] &= 0xFFFFFFFE;
171 } else if (trailingBits == 1) {
172 checkSequence[totalInts-1] = 0;
173 } else {
174 // trailingBits include heading and trailing zero bits
175 int mask = 0x80000000 >> trailingBits-2; // with sign!
176 checkSequence[totalInts - 1] &= mask;
177 }
178 for (int e : checkSequence) {
179 System.out.print(toBinaryString(e, ' '));
180 }
181 System.out.println();
182 }
183 datas.put(x[0], data);
184 }
185 }
186 }
187
188 System.out.println("Total queries:" + totalQueries);
189 for (int x : finalSequence) {
190 System.out.print(toBinaryString(x, ' '));
191 }
192 }
193
194 private void populate(int root, int head, int[] data, int[] finalSequence) {
195 for (int i = 1, x = 0; root+i < head; i = i << 1, x++) {
196 int value = data[x];
197 int value_check = root+i;
198 if (value != value_check) {
199 throw new IllegalStateException();
200 }
201 int wordIx = (root + i) >>> 5;
202 int bitIx = (root + i) & 0x1f;
203 finalSequence[wordIx] |= 1 << (31-bitIx);
204 }
205 }
206
207 private static String toBinaryString(int x, char byteSeparator) {
208 StringBuilder sb = new StringBuilder(4*8+4);
209 sb.append(toBinaryString((byte) (x >>> 24)));
210 sb.append(byteSeparator);
211 sb.append(toBinaryString((byte) ((x & 0x00ff0000) >>> 16)));
212 sb.append(byteSeparator);
213 sb.append(toBinaryString((byte) ((x & 0x00ff00) >>> 8)));
214 sb.append(byteSeparator);
215 sb.append(toBinaryString((byte) (x & 0x00ff)));
216 sb.append(byteSeparator);
217 return sb.toString();
218 }
219
220 private static String toBinaryString(byte b) {
221 final String nibbles = "0000000100100011010001010110011110001001101010111100110111101111";
222 assert nibbles.length() == 16*4;
223 int x1 = (b >>> 4) & 0x0f, x2 = b & 0x0f;
224 x1 *= 4; x2 *= 4; // 4 characters per nibble
225 return nibbles.substring(x1, x1+4).concat(nibbles.substring(x2, x2+4));
226 }
227 }
228 }