| 1 |
"""revset to select sample of repository |
| 2 |
|
| 3 |
Hopefully this is useful to create interesting discovery cases. |
| 4 |
""" |
| 5 |
|
| 6 |
import collections |
| 7 |
import random |
| 8 |
import sortedcontainers |
| 9 |
|
| 10 |
from mercurial.i18n import _ |
| 11 |
|
| 12 |
from mercurial import ( |
| 13 |
registrar, |
| 14 |
revset, |
| 15 |
revsetlang, |
| 16 |
smartset, |
| 17 |
) |
| 18 |
|
| 19 |
SortedSet = sortedcontainers.SortedSet |
| 20 |
|
| 21 |
revsetpredicate = registrar.revsetpredicate() |
| 22 |
|
| 23 |
|
| 24 |
@revsetpredicate(b'subsetspec("<spec>")') |
| 25 |
def subsetmarkerspec(repo, subset, x): |
| 26 |
"""use a shorthand spec as used by search-discovery-case |
| 27 |
|
| 28 |
Supported format are: |
| 29 |
|
| 30 |
- "scratch-count-seed": not scratch(all(), count, "seed") |
| 31 |
- "randomantichain-seed": ::randomantichain(all(), "seed") |
| 32 |
- "rev-REV": "::REV" |
| 33 |
""" |
| 34 |
args = revsetlang.getargs( |
| 35 |
x, 0, 1, _(b'subsetspec("spec") required an argument') |
| 36 |
) |
| 37 |
|
| 38 |
spec = revsetlang.getstring(args[0], _(b"spec should be a string")) |
| 39 |
case = spec.split(b'-') |
| 40 |
t = case[0] |
| 41 |
if t == b'scratch': |
| 42 |
spec_revset = b'not scratch(all(), %s, "%s")' % (case[1], case[2]) |
| 43 |
elif t == b'randomantichain': |
| 44 |
spec_revset = b'::randomantichain(all(), "%s")' % case[1] |
| 45 |
elif t == b'rev': |
| 46 |
spec_revset = b'::%d' % case[1] |
| 47 |
else: |
| 48 |
assert False, spec |
| 49 |
|
| 50 |
selected = repo.revs(spec_revset) |
| 51 |
|
| 52 |
return selected & subset |
| 53 |
|
| 54 |
|
| 55 |
@revsetpredicate(b'scratch(REVS, <count>, [seed])') |
| 56 |
def scratch(repo, subset, x): |
| 57 |
"""randomly remove <count> revision from the repository top |
| 58 |
|
| 59 |
This subset is created by recursively picking changeset starting from the |
| 60 |
heads. It can be summarized using the following algorithm:: |
| 61 |
|
| 62 |
selected = set() |
| 63 |
for i in range(<count>): |
| 64 |
unselected = repo.revs("not <selected>") |
| 65 |
candidates = repo.revs("heads(<unselected>)") |
| 66 |
pick = random.choice(candidates) |
| 67 |
selected.add(pick) |
| 68 |
""" |
| 69 |
m = _(b"scratch expects revisions, count argument and an optional seed") |
| 70 |
args = revsetlang.getargs(x, 2, 3, m) |
| 71 |
if len(args) == 2: |
| 72 |
x, n = args |
| 73 |
rand = random |
| 74 |
elif len(args) == 3: |
| 75 |
x, n, seed = args |
| 76 |
seed = revsetlang.getinteger(seed, _(b"seed should be a number")) |
| 77 |
rand = random.Random(seed) |
| 78 |
else: |
| 79 |
assert False |
| 80 |
|
| 81 |
n = revsetlang.getinteger(n, _(b"scratch expects a number")) |
| 82 |
|
| 83 |
selected = set() |
| 84 |
heads = SortedSet() |
| 85 |
children_count = collections.defaultdict(int) |
| 86 |
parents = repo.changelog._uncheckedparentrevs |
| 87 |
|
| 88 |
baseset = revset.getset(repo, smartset.fullreposet(repo), x) |
| 89 |
baseset.sort() |
| 90 |
for r in baseset: |
| 91 |
heads.add(r) |
| 92 |
|
| 93 |
p1, p2 = parents(r) |
| 94 |
if p1 >= 0: |
| 95 |
heads.discard(p1) |
| 96 |
children_count[p1] += 1 |
| 97 |
if p2 >= 0: |
| 98 |
heads.discard(p2) |
| 99 |
children_count[p2] += 1 |
| 100 |
|
| 101 |
for h in heads: |
| 102 |
assert children_count[h] == 0 |
| 103 |
|
| 104 |
selected = set() |
| 105 |
for x in range(n): |
| 106 |
if not heads: |
| 107 |
break |
| 108 |
pick = rand.choice(heads) |
| 109 |
heads.remove(pick) |
| 110 |
assert pick not in selected |
| 111 |
selected.add(pick) |
| 112 |
p1, p2 = parents(pick) |
| 113 |
if p1 in children_count: |
| 114 |
assert p1 in children_count |
| 115 |
children_count[p1] -= 1 |
| 116 |
assert children_count[p1] >= 0 |
| 117 |
if children_count[p1] == 0: |
| 118 |
assert p1 not in selected, (r, p1) |
| 119 |
heads.add(p1) |
| 120 |
if p2 in children_count: |
| 121 |
assert p2 in children_count |
| 122 |
children_count[p2] -= 1 |
| 123 |
assert children_count[p2] >= 0 |
| 124 |
if children_count[p2] == 0: |
| 125 |
assert p2 not in selected, (r, p2) |
| 126 |
heads.add(p2) |
| 127 |
|
| 128 |
return smartset.baseset(selected) & subset |
| 129 |
|
| 130 |
|
| 131 |
@revsetpredicate(b'randomantichain(REVS, [seed])') |
| 132 |
def antichain(repo, subset, x): |
| 133 |
"""Pick a random anti-chain in the repository |
| 134 |
|
| 135 |
A antichain is a set of changeset where there isn't any element that is |
| 136 |
either a descendant or ancestors of any other element in the set. In other |
| 137 |
word, all the elements are independant. It can be summarized with the |
| 138 |
following algorithm:: |
| 139 |
|
| 140 |
selected = set() |
| 141 |
unselected = repo.revs('all()') |
| 142 |
while unselected: |
| 143 |
pick = random.choice(unselected) |
| 144 |
selected.add(pick) |
| 145 |
unselected -= repo.revs('::<pick> + <pick>::') |
| 146 |
""" |
| 147 |
|
| 148 |
args = revsetlang.getargs( |
| 149 |
x, 1, 2, _(b"randomantichain expects revisions and an optional seed") |
| 150 |
) |
| 151 |
if len(args) == 1: |
| 152 |
(x,) = args |
| 153 |
rand = random |
| 154 |
elif len(args) == 2: |
| 155 |
x, seed = args |
| 156 |
seed = revsetlang.getinteger(seed, _(b"seed should be a number")) |
| 157 |
rand = random.Random(seed) |
| 158 |
else: |
| 159 |
assert False |
| 160 |
|
| 161 |
cl = repo.changelog |
| 162 |
|
| 163 |
# We already have cheap access to the parent mapping. |
| 164 |
# However, we need to build a mapping of the children mapping |
| 165 |
parents = repo.changelog._uncheckedparentrevs |
| 166 |
children_map = collections.defaultdict(list) |
| 167 |
for r in cl: |
| 168 |
p1, p2 = parents(r) |
| 169 |
if p1 >= 0: |
| 170 |
children_map[p1].append(r) |
| 171 |
if p2 >= 0: |
| 172 |
children_map[p2].append(r) |
| 173 |
children = children_map.__getitem__ |
| 174 |
|
| 175 |
selected = set() |
| 176 |
undecided = SortedSet(cl) |
| 177 |
|
| 178 |
while undecided: |
| 179 |
# while there is "undecided content", we pick a random changeset X |
| 180 |
# and we remove anything in `::X + X::` from undecided content |
| 181 |
pick = rand.choice(undecided) |
| 182 |
selected.add(pick) |
| 183 |
undecided.remove(pick) |
| 184 |
|
| 185 |
ancestors = {p for p in parents(pick) if p in undecided} |
| 186 |
descendants = {c for c in children(pick) if c in undecided} |
| 187 |
|
| 188 |
while ancestors: |
| 189 |
current = ancestors.pop() |
| 190 |
undecided.remove(current) |
| 191 |
for p in parents(current): |
| 192 |
if p in undecided: |
| 193 |
ancestors.add(p) |
| 194 |
while descendants: |
| 195 |
current = descendants.pop() |
| 196 |
undecided.remove(current) |
| 197 |
for p in children(current): |
| 198 |
if p in undecided: |
| 199 |
ancestors.add(p) |
| 200 |
|
| 201 |
return smartset.baseset(selected) & subset |