Show More
@@ -1,94 +1,139 b'' | |||
|
1 | 1 | """revset to select sample of repository |
|
2 | 2 | |
|
3 | 3 | Hopefully this is useful to create interesting discovery cases. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | import collections |
|
7 | 7 | import random |
|
8 | 8 | |
|
9 | 9 | from mercurial.i18n import _ |
|
10 | 10 | |
|
11 | 11 | from mercurial import ( |
|
12 | 12 | registrar, |
|
13 | 13 | revset, |
|
14 | 14 | revsetlang, |
|
15 | 15 | smartset, |
|
16 | 16 | ) |
|
17 | 17 | |
|
18 | 18 | revsetpredicate = registrar.revsetpredicate() |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | @revsetpredicate(b'scratch(REVS, <count>, [seed])') |
|
22 | 22 | def scratch(repo, subset, x): |
|
23 | 23 | """randomly remove <count> revision from the repository top |
|
24 | 24 | |
|
25 | 25 | This subset is created by recursively picking changeset starting from the |
|
26 | 26 | heads. It can be summarized using the following algorithm:: |
|
27 | 27 | |
|
28 | 28 | selected = set() |
|
29 | 29 | for i in range(<count>): |
|
30 | 30 | unselected = repo.revs("not <selected>") |
|
31 | 31 | candidates = repo.revs("heads(<unselected>)") |
|
32 | 32 | pick = random.choice(candidates) |
|
33 | 33 | selected.add(pick) |
|
34 | 34 | """ |
|
35 | 35 | m = _(b"scratch expects revisions, count argument and an optional seed") |
|
36 | 36 | args = revsetlang.getargs(x, 2, 3, m) |
|
37 | 37 | if len(args) == 2: |
|
38 | 38 | x, n = args |
|
39 | 39 | rand = random |
|
40 | 40 | elif len(args) == 3: |
|
41 | 41 | x, n, seed = args |
|
42 | 42 | seed = revsetlang.getinteger(seed, _(b"seed should be a number")) |
|
43 | 43 | rand = random.Random(seed) |
|
44 | 44 | else: |
|
45 | 45 | assert False |
|
46 | 46 | |
|
47 | 47 | n = revsetlang.getinteger(n, _(b"scratch expects a number")) |
|
48 | 48 | |
|
49 | 49 | selected = set() |
|
50 | 50 | heads = set() |
|
51 | 51 | children_count = collections.defaultdict(lambda: 0) |
|
52 | 52 | parents = repo.changelog._uncheckedparentrevs |
|
53 | 53 | |
|
54 | 54 | baseset = revset.getset(repo, smartset.fullreposet(repo), x) |
|
55 | 55 | baseset.sort() |
|
56 | 56 | for r in baseset: |
|
57 | 57 | heads.add(r) |
|
58 | 58 | |
|
59 | 59 | p1, p2 = parents(r) |
|
60 | 60 | if p1 >= 0: |
|
61 | 61 | heads.discard(p1) |
|
62 | 62 | children_count[p1] += 1 |
|
63 | 63 | if p2 >= 0: |
|
64 | 64 | heads.discard(p2) |
|
65 | 65 | children_count[p2] += 1 |
|
66 | 66 | |
|
67 | 67 | for h in heads: |
|
68 | 68 | assert children_count[h] == 0 |
|
69 | 69 | |
|
70 | 70 | selected = set() |
|
71 | 71 | for x in range(n): |
|
72 | 72 | if not heads: |
|
73 | 73 | break |
|
74 | 74 | pick = rand.choice(list(heads)) |
|
75 | 75 | heads.remove(pick) |
|
76 | 76 | assert pick not in selected |
|
77 | 77 | selected.add(pick) |
|
78 | 78 | p1, p2 = parents(pick) |
|
79 | 79 | if p1 in children_count: |
|
80 | 80 | assert p1 in children_count |
|
81 | 81 | children_count[p1] -= 1 |
|
82 | 82 | assert children_count[p1] >= 0 |
|
83 | 83 | if children_count[p1] == 0: |
|
84 | 84 | assert p1 not in selected, (r, p1) |
|
85 | 85 | heads.add(p1) |
|
86 | 86 | if p2 in children_count: |
|
87 | 87 | assert p2 in children_count |
|
88 | 88 | children_count[p2] -= 1 |
|
89 | 89 | assert children_count[p2] >= 0 |
|
90 | 90 | if children_count[p2] == 0: |
|
91 | 91 | assert p2 not in selected, (r, p2) |
|
92 | 92 | heads.add(p2) |
|
93 | 93 | |
|
94 | 94 | return smartset.baseset(selected) & subset |
|
95 | ||
|
96 | ||
|
97 | @revsetpredicate(b'randomantichain(REVS, [seed])') | |
|
98 | def antichain(repo, subset, x): | |
|
99 | """Pick a random anti-chain in the repository | |
|
100 | ||
|
101 | A antichain is a set of changeset where there isn't any element that is | |
|
102 | either a descendant or ancestors of any other element in the set. In other | |
|
103 | word, all the elements are independant. It can be summarized with the | |
|
104 | following algorithm:: | |
|
105 | ||
|
106 | selected = set() | |
|
107 | unselected = repo.revs('all()') | |
|
108 | while unselected: | |
|
109 | pick = random.choice(unselected) | |
|
110 | selected.add(pick) | |
|
111 | unselected -= repo.revs('::<pick> + <pick>::') | |
|
112 | """ | |
|
113 | ||
|
114 | args = revsetlang.getargs( | |
|
115 | x, 1, 2, _(b"randomantichain expects revisions and an optional seed") | |
|
116 | ) | |
|
117 | if len(args) == 1: | |
|
118 | (x,) = args | |
|
119 | rand = random | |
|
120 | elif len(args) == 2: | |
|
121 | x, seed = args | |
|
122 | seed = revsetlang.getinteger(seed, _(b"seed should be a number")) | |
|
123 | rand = random.Random(seed) | |
|
124 | else: | |
|
125 | assert False | |
|
126 | ||
|
127 | selected = set() | |
|
128 | ||
|
129 | baseset = revset.getset(repo, smartset.fullreposet(repo), x) | |
|
130 | undecided = baseset | |
|
131 | ||
|
132 | while undecided: | |
|
133 | pick = rand.choice(list(undecided)) | |
|
134 | selected.add(pick) | |
|
135 | undecided = repo.revs( | |
|
136 | '%ld and not (::%ld or %ld::head())', baseset, selected, selected | |
|
137 | ) | |
|
138 | ||
|
139 | return smartset.baseset(selected) & subset |
General Comments 0
You need to be logged in to leave comments.
Login now