tikhomirov@31: /* tikhomirov@74: * Copyright (c) 2011 TMate Software Ltd tikhomirov@74: * tikhomirov@74: * This program is free software; you can redistribute it and/or modify tikhomirov@74: * it under the terms of the GNU General Public License as published by tikhomirov@74: * the Free Software Foundation; version 2 of the License. tikhomirov@74: * tikhomirov@74: * This program is distributed in the hope that it will be useful, tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@74: * GNU General Public License for more details. tikhomirov@74: * tikhomirov@74: * For information on how to redistribute this software under tikhomirov@74: * the terms of a license other than GNU General Public License tikhomirov@102: * contact TMate Software at support@hg4j.com tikhomirov@31: */ tikhomirov@74: package org.tmatesoft.hg.console; tikhomirov@31: tikhomirov@178: import java.util.ArrayList; tikhomirov@173: import java.util.Arrays; tikhomirov@173: import java.util.Collections; tikhomirov@173: import java.util.Comparator; tikhomirov@31: import java.util.HashSet; tikhomirov@173: import java.util.Iterator; tikhomirov@173: import java.util.LinkedHashMap; tikhomirov@184: import java.util.LinkedHashSet; tikhomirov@31: import java.util.LinkedList; tikhomirov@31: import java.util.List; tikhomirov@178: import java.util.ListIterator; tikhomirov@173: import java.util.Map.Entry; tikhomirov@173: tikhomirov@184: import org.tmatesoft.hg.core.HgBadStateException; tikhomirov@176: import org.tmatesoft.hg.core.HgException; tikhomirov@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@181: import org.tmatesoft.hg.internal.RepositoryComparator; tikhomirov@181: import org.tmatesoft.hg.internal.RepositoryComparator.BranchChain; tikhomirov@184: import org.tmatesoft.hg.repo.HgBundle; tikhomirov@97: import org.tmatesoft.hg.repo.HgChangelog; tikhomirov@176: import org.tmatesoft.hg.repo.HgLookup; tikhomirov@176: import org.tmatesoft.hg.repo.HgRemoteRepository; tikhomirov@74: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@184: import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; tikhomirov@74: tikhomirov@31: tikhomirov@31: /** tikhomirov@74: * WORK IN PROGRESS, DO NOT USE tikhomirov@173: * hg incoming counterpart tikhomirov@74: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@31: */ tikhomirov@31: public class Incoming { tikhomirov@31: tikhomirov@31: public static void main(String[] args) throws Exception { tikhomirov@176: if (Boolean.FALSE.booleanValue()) { tikhomirov@173: new SequenceConstructor().test(); tikhomirov@173: return; tikhomirov@173: } tikhomirov@74: Options cmdLineOpts = Options.parse(args); tikhomirov@184: final HgRepository hgRepo = cmdLineOpts.findRepository(); tikhomirov@31: if (hgRepo.isInvalid()) { tikhomirov@31: System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); tikhomirov@31: return; tikhomirov@31: } tikhomirov@184: HgRemoteRepository hgRemote = new HgLookup().detectRemote(cmdLineOpts.getSingle(""), hgRepo); tikhomirov@181: if (hgRemote.isInvalid()) { tikhomirov@181: System.err.printf("Remote repository %s is not valid", hgRemote.getLocation()); tikhomirov@181: return; tikhomirov@176: } tikhomirov@176: // tikhomirov@31: // in fact, all we need from changelog is set of all nodeids. However, since ParentWalker reuses same Nodeids, it's not too expensive tikhomirov@31: // to reuse it here, XXX although later this may need to be refactored tikhomirov@98: final HgChangelog.ParentWalker pw = hgRepo.getChangelog().new ParentWalker(); tikhomirov@31: pw.init(); tikhomirov@31: // tikhomirov@181: RepositoryComparator repoCompare = new RepositoryComparator(pw, hgRemote); tikhomirov@181: repoCompare.compare(null); tikhomirov@181: List missingBranches0 = repoCompare.calculateMissingBranches(); tikhomirov@184: final LinkedHashSet common = new LinkedHashSet(); tikhomirov@184: // XXX common can be obtained from repoCompare, but at the moment it would almost duplicate work of calculateMissingBranches tikhomirov@184: // once I refactor latter, common shall be taken from repoCompare. tikhomirov@178: for (BranchChain bc : missingBranches0) { tikhomirov@178: bc.dump(); tikhomirov@184: common.add(bc.branchRoot); // common known node tikhomirov@181: List missing = visitBranches(repoCompare, bc); tikhomirov@184: assert bc.branchRoot.equals(missing.get(0)); tikhomirov@184: missing.remove(0); tikhomirov@184: Collections.reverse(missing); // useful to test output, from newer to older tikhomirov@184: System.out.println("Nodes to fetch in this branch:"); tikhomirov@178: for (Nodeid n : missing) { tikhomirov@178: if (pw.knownNode(n)) { tikhomirov@178: System.out.println("Erroneous to fetch:" + n); tikhomirov@178: } else { tikhomirov@178: System.out.println(n); tikhomirov@178: } tikhomirov@178: } tikhomirov@178: System.out.println("Branch done"); tikhomirov@31: } tikhomirov@184: // tikhomirov@184: // Complete tikhomirov@184: HgBundle changegroup = hgRemote.getChanges(new LinkedList(common)); tikhomirov@184: changegroup.changes(hgRepo, new HgChangelog.Inspector() { tikhomirov@184: private int localIndex; tikhomirov@184: tikhomirov@184: public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) { tikhomirov@184: if (pw.knownNode(nodeid)) { tikhomirov@184: if (!common.contains(nodeid)) { tikhomirov@184: throw new HgBadStateException("Bundle shall not report known nodes other than roots we've supplied"); tikhomirov@184: } tikhomirov@184: localIndex = hgRepo.getChangelog().getLocalRevision(nodeid); tikhomirov@184: return; tikhomirov@184: } tikhomirov@184: System.out.printf("changeset: %d:%s\n", ++localIndex, nodeid.toString()); tikhomirov@184: System.out.printf("user: %s\n", cset.user()); tikhomirov@184: System.out.printf("date: %s\n", cset.dateString()); tikhomirov@184: System.out.printf("comment: %s\n\n", cset.comment()); tikhomirov@184: tikhomirov@184: } tikhomirov@184: }); tikhomirov@178: } tikhomirov@178: tikhomirov@184: // returns in order from branch root to head tikhomirov@184: // for a non-empty BranchChain, shall return modifiable list tikhomirov@181: private static List visitBranches(RepositoryComparator repoCompare, BranchChain bc) throws HgException { tikhomirov@178: if (bc == null) { tikhomirov@178: return Collections.emptyList(); tikhomirov@178: } tikhomirov@181: List mine = repoCompare.completeBranch(bc.branchRoot, bc.branchHead); tikhomirov@178: if (bc.isTerminal()) { tikhomirov@178: return mine; tikhomirov@178: } tikhomirov@181: List parentBranch1 = visitBranches(repoCompare, bc.p1); tikhomirov@181: List parentBranch2 = visitBranches(repoCompare, bc.p2); tikhomirov@178: // merge tikhomirov@178: LinkedList merged = new LinkedList(); tikhomirov@178: ListIterator i1 = parentBranch1.listIterator(), i2 = parentBranch2.listIterator(); tikhomirov@178: while (i1.hasNext() && i2.hasNext()) { tikhomirov@178: Nodeid n1 = i1.next(); tikhomirov@178: Nodeid n2 = i2.next(); tikhomirov@178: if (n1.equals(n2)) { tikhomirov@178: merged.addLast(n1); tikhomirov@178: } else { tikhomirov@178: // first different => add both, and continue adding both tails sequentially tikhomirov@178: merged.add(n2); tikhomirov@178: merged.add(n1); tikhomirov@178: break; tikhomirov@178: } tikhomirov@178: } tikhomirov@178: // copy rest of second parent branch tikhomirov@178: while (i2.hasNext()) { tikhomirov@178: merged.add(i2.next()); tikhomirov@178: } tikhomirov@178: // copy rest of first parent branch tikhomirov@178: while (i1.hasNext()) { tikhomirov@178: merged.add(i1.next()); tikhomirov@178: } tikhomirov@31: // tikhomirov@178: ArrayList rv = new ArrayList(mine.size() + merged.size()); tikhomirov@178: rv.addAll(merged); tikhomirov@178: rv.addAll(mine); tikhomirov@178: return rv; tikhomirov@178: } tikhomirov@31: tikhomirov@31: tikhomirov@173: private static class SequenceConstructor { tikhomirov@173: tikhomirov@173: private int[] between(int root, int head) { tikhomirov@173: if (head <= (root+1)) { tikhomirov@173: return new int[0]; tikhomirov@173: } tikhomirov@173: System.out.printf("[%d, %d]\t\t", root, head); tikhomirov@173: int size = 1 + (int) Math.floor(Math.log(head-root - 1) / Math.log(2)); tikhomirov@173: int[] rv = new int[size]; tikhomirov@173: for (int v = 1, i = 0; i < rv.length; i++) { tikhomirov@173: rv[i] = root + v; tikhomirov@173: v = v << 1; tikhomirov@173: } tikhomirov@173: System.out.println(Arrays.toString(rv)); tikhomirov@173: return rv; tikhomirov@173: } tikhomirov@173: tikhomirov@173: public void test() { tikhomirov@176: int root = 0, head = 126; tikhomirov@176: int[] data = between(root, head); // max number of elements to recover is 2**data.length-1, when head is exactly tikhomirov@176: // 2**data.length element of the branch. tikhomirov@176: // In such case, total number of elements in the branch (including head and root, would be 2**data.length+1 tikhomirov@176: int[] finalSequence = new int[1 + (1 << data.length >>> 5)]; // div 32 - total bits to integers, +1 for possible modulus tikhomirov@173: int exactNumberOfElements = -1; // exact number of meaningful bits in finalSequence tikhomirov@173: LinkedHashMap datas = new LinkedHashMap(); tikhomirov@173: datas.put(root, data); tikhomirov@173: int totalQueries = 1; tikhomirov@173: HashSet queried = new HashSet(); tikhomirov@173: int[] checkSequence = null; tikhomirov@173: while(!datas.isEmpty()) { tikhomirov@173: LinkedList toQuery = new LinkedList(); tikhomirov@173: do { tikhomirov@173: Iterator> it = datas.entrySet().iterator(); tikhomirov@173: Entry next = it.next(); tikhomirov@173: int r = next.getKey(); tikhomirov@173: data = next.getValue(); tikhomirov@173: it.remove(); tikhomirov@173: populate(r, head, data, finalSequence); tikhomirov@173: if (checkSequence != null) { tikhomirov@173: boolean match = true; tikhomirov@173: // System.out.println("Try to match:"); tikhomirov@173: for (int i = 0; i < checkSequence.length; i++) { tikhomirov@173: // System.out.println(i); tikhomirov@173: // System.out.println("control:" + toBinaryString(checkSequence[i], ' ')); tikhomirov@173: // System.out.println("present:" + toBinaryString(finalSequence[i], ' ')); tikhomirov@173: if (checkSequence[i] != finalSequence[i]) { tikhomirov@173: match = false; tikhomirov@173: } else { tikhomirov@173: match &= true; tikhomirov@173: } tikhomirov@173: } tikhomirov@173: System.out.println(match ? "Match, on query:" + totalQueries : "Didn't match"); tikhomirov@173: } tikhomirov@176: if (data.length > 1) { tikhomirov@176: /*queries for elements next to head is senseless, hence data.length check above and head-x below*/ tikhomirov@173: for (int x : data) { tikhomirov@176: if (!queried.contains(x) && head - x > 1) { tikhomirov@173: toQuery.add(new int[] {x, head}); tikhomirov@173: } tikhomirov@173: } tikhomirov@173: } tikhomirov@173: } while (!datas.isEmpty()) ; tikhomirov@173: if (!toQuery.isEmpty()) { tikhomirov@173: System.out.println(); tikhomirov@173: totalQueries++; tikhomirov@173: } tikhomirov@173: Collections.sort(toQuery, new Comparator() { tikhomirov@173: tikhomirov@173: public int compare(int[] o1, int[] o2) { tikhomirov@173: return o1[0] < o2[0] ? -1 : (o1[0] == o2[0] ? 0 : 1); tikhomirov@173: } tikhomirov@173: }); tikhomirov@173: for (int[] x : toQuery) { tikhomirov@173: if (!queried.contains(x[0])) { tikhomirov@173: queried.add(x[0]); tikhomirov@173: data = between(x[0], x[1]); tikhomirov@173: if (exactNumberOfElements == -1 && data.length == 1) { tikhomirov@173: exactNumberOfElements = x[0] + 1; tikhomirov@173: System.out.printf("On query %d found out exact number of missing elements: %d\n", totalQueries, exactNumberOfElements); tikhomirov@173: // get a bit sequence of exactNumberOfElements, 0111..110 tikhomirov@173: // to 'and' it with finalSequence later tikhomirov@173: int totalInts = (exactNumberOfElements + 2 /*heading and tailing zero bits*/) >>> 5; tikhomirov@173: int trailingBits = (exactNumberOfElements + 2) & 0x1f; tikhomirov@173: if (trailingBits != 0) { tikhomirov@173: totalInts++; tikhomirov@173: } tikhomirov@173: checkSequence = new int[totalInts]; tikhomirov@173: Arrays.fill(checkSequence, 0xffffffff); tikhomirov@173: checkSequence[0] &= 0x7FFFFFFF; tikhomirov@173: if (trailingBits == 0) { tikhomirov@173: checkSequence[totalInts-1] &= 0xFFFFFFFE; tikhomirov@173: } else if (trailingBits == 1) { tikhomirov@173: checkSequence[totalInts-1] = 0; tikhomirov@173: } else { tikhomirov@173: // trailingBits include heading and trailing zero bits tikhomirov@173: int mask = 0x80000000 >> trailingBits-2; // with sign! tikhomirov@173: checkSequence[totalInts - 1] &= mask; tikhomirov@173: } tikhomirov@173: for (int e : checkSequence) { tikhomirov@173: System.out.print(toBinaryString(e, ' ')); tikhomirov@173: } tikhomirov@173: System.out.println(); tikhomirov@173: } tikhomirov@173: datas.put(x[0], data); tikhomirov@173: } tikhomirov@173: } tikhomirov@173: } tikhomirov@173: tikhomirov@173: System.out.println("Total queries:" + totalQueries); tikhomirov@173: for (int x : finalSequence) { tikhomirov@173: System.out.print(toBinaryString(x, ' ')); tikhomirov@173: } tikhomirov@173: } tikhomirov@173: tikhomirov@173: private void populate(int root, int head, int[] data, int[] finalSequence) { tikhomirov@173: for (int i = 1, x = 0; root+i < head; i = i << 1, x++) { tikhomirov@173: int value = data[x]; tikhomirov@173: int value_check = root+i; tikhomirov@174: if (value != value_check) { tikhomirov@174: throw new IllegalStateException(); tikhomirov@174: } tikhomirov@173: int wordIx = (root + i) >>> 5; tikhomirov@173: int bitIx = (root + i) & 0x1f; tikhomirov@173: finalSequence[wordIx] |= 1 << (31-bitIx); tikhomirov@173: } tikhomirov@173: } tikhomirov@173: tikhomirov@173: private static String toBinaryString(int x, char byteSeparator) { tikhomirov@173: StringBuilder sb = new StringBuilder(4*8+4); tikhomirov@173: sb.append(toBinaryString((byte) (x >>> 24))); tikhomirov@173: sb.append(byteSeparator); tikhomirov@173: sb.append(toBinaryString((byte) ((x & 0x00ff0000) >>> 16))); tikhomirov@173: sb.append(byteSeparator); tikhomirov@173: sb.append(toBinaryString((byte) ((x & 0x00ff00) >>> 8))); tikhomirov@173: sb.append(byteSeparator); tikhomirov@173: sb.append(toBinaryString((byte) (x & 0x00ff))); tikhomirov@173: sb.append(byteSeparator); tikhomirov@173: return sb.toString(); tikhomirov@173: } tikhomirov@173: tikhomirov@173: private static String toBinaryString(byte b) { tikhomirov@173: final String nibbles = "0000000100100011010001010110011110001001101010111100110111101111"; tikhomirov@173: assert nibbles.length() == 16*4; tikhomirov@173: int x1 = (b >>> 4) & 0x0f, x2 = b & 0x0f; tikhomirov@173: x1 *= 4; x2 *= 4; // 4 characters per nibble tikhomirov@173: return nibbles.substring(x1, x1+4).concat(nibbles.substring(x2, x2+4)); tikhomirov@173: } tikhomirov@173: } tikhomirov@31: }