comparison src/org/tmatesoft/hg/internal/RevisionDescendants.java @ 471:7bcfbc255f48

Merge changes from smartgit3 branch into 1.1 stream
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 11 Jul 2012 20:40:47 +0200
parents 03fd8d079e9c
children 2a0b09eec376
comparison
equal deleted inserted replaced
470:31bd09da0dcf 471:7bcfbc255f48
1 /*
2 * Copyright (c) 2012 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.internal;
18
19 import java.util.BitSet;
20
21 import org.tmatesoft.hg.core.Nodeid;
22 import org.tmatesoft.hg.repo.HgChangelog;
23 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
24 import org.tmatesoft.hg.repo.HgInvalidStateException;
25 import org.tmatesoft.hg.repo.HgRepository;
26
27 /**
28 * Represent indicators which revisions are descendants of the supplied root revision
29 * This is sort of lightweight alternative to ParentWalker#childrenOf
30 *
31 * @author Artem Tikhomirov
32 * @author TMate Software Ltd.
33 */
34 public class RevisionDescendants {
35
36 private final HgRepository repo;
37 private final int rootRevIndex;
38 private final int tipRevIndex; // this is the last revision we cache to
39 private final BitSet descendants;
40
41 // in fact, may be refactored to deal not only with changelog, but any revlog (not sure what would be the usecase, though)
42 public RevisionDescendants(HgRepository hgRepo, int revisionIndex) {
43 repo = hgRepo;
44 rootRevIndex = revisionIndex;
45 // even if tip moves, we still answer correctly for those isCandidate()
46 tipRevIndex = repo.getChangelog().getLastRevision();
47 if (revisionIndex < 0 || revisionIndex > tipRevIndex) {
48 String m = "Revision to build descendants for shall be in range [%d,%d], not %d";
49 throw new IllegalArgumentException(String.format(m, 0, tipRevIndex, revisionIndex));
50 }
51 descendants = new BitSet(tipRevIndex - rootRevIndex + 1);
52 }
53
54 public void build() throws HgInvalidControlFileException {
55 final BitSet result = descendants;
56 result.set(0);
57 if (rootRevIndex == tipRevIndex) {
58 return;
59 }
60 repo.getChangelog().indexWalk(rootRevIndex+1, tipRevIndex, new HgChangelog.ParentInspector() {
61 // TODO ParentRevisionInspector, with no parent nodeids, just indexes?
62
63 private int i = 1; // above we start with revision next to rootRevIndex, which is at offset 0
64 public void next(int revisionIndex, Nodeid revision, int parent1, int parent2, Nodeid nidParent1, Nodeid nidParent2) {
65 int p1x = parent1 - rootRevIndex;
66 int p2x = parent2 - rootRevIndex;
67 boolean p1IsDescendant = false, p2IsDescendant = false;
68 if (p1x >= 0) { // parent1 is among descendants candidates
69 assert p1x < result.size();
70 p1IsDescendant = result.get(p1x);
71 }
72 if (p2x >= 0) {
73 assert p2x < result.size();
74 p2IsDescendant = result.get(p2x);
75 }
76 //
77 int rx = revisionIndex - rootRevIndex;
78 if (rx != i) {
79 throw new HgInvalidStateException(String.format("Sanity check failed. Revision %d. Expected:%d, was:%d", revisionIndex, rx, i));
80 }
81 // current revision is descendant if any of its parents is descendant
82 result.set(rx, p1IsDescendant || p2IsDescendant);
83 i++;
84 }
85 });
86 }
87
88 // deliberately doesn't allow TIP
89 public boolean isCandidate(int revIndex) {
90 return (revIndex >= rootRevIndex && revIndex <= tipRevIndex) ;
91 }
92
93 public boolean hasDescendants() { // isEmpty is better name?
94 // bit at rootRevIndex is always set
95 return descendants.nextSetBit(rootRevIndex+1) != -1;
96 }
97
98 public boolean isDescendant(int revisionIndex) {
99 assert isCandidate(revisionIndex);
100 int ix = revisionIndex - rootRevIndex;
101 assert ix < descendants.size();
102 return descendants.get(ix);
103 }
104 }