##// END OF EJS Templates
Make bisect extension work with python2.3
Thomas Arendsen Hein -
r1856:b8bd84ad default
parent child Browse files
Show More
@@ -1,286 +1,287
1 1 # bisect extension for mercurial
2 2 #
3 3 # This software may be used and distributed according to the terms
4 4 # of the GNU General Public License, incorporated herein by reference.
5 5
6 6 from mercurial.demandload import demandload
7 7 demandload(globals(), "os sys sets mercurial:hg,util")
8 8
9 9 versionstr = "0.0.3"
10 10
11 11 def lookup_rev(ui, repo, rev=None):
12 12 """returns rev or the checked-out revision if rev is None"""
13 13 if not rev is None:
14 14 return repo.lookup(rev)
15 15 parents = [p for p in repo.dirstate.parents() if p != hg.nullid]
16 16 if len(parents) != 1:
17 17 ui.warn("unexpected number of parents\n")
18 18 ui.warn("please commit or revert\n")
19 19 sys.exit(1)
20 20 return parents.pop()
21 21
22 22 def check_clean(ui, repo):
23 23 modified, added, removed, deleted, unknown = repo.changes()
24 24 if modified or added or removed:
25 25 ui.warn("Repository is not clean, please commit or revert\n")
26 26 sys.exit(1)
27 27
28 28 class bisect(object):
29 29 """dichotomic search in the DAG of changesets"""
30 30 def __init__(self, ui, repo):
31 31 self.repo = repo
32 32 self.path = repo.join("bisect")
33 33 self.opener = util.opener(self.path)
34 34 self.ui = ui
35 35 self.goodrevs = []
36 36 self.badrev = None
37 37 self.good_dirty = 0
38 38 self.bad_dirty = 0
39 39 self.good_path = "good"
40 40 self.bad_path = "bad"
41 41
42 42 if os.path.exists(os.path.join(self.path, self.good_path)):
43 43 self.goodrevs = self.opener(self.good_path).read().splitlines()
44 44 self.goodrevs = [hg.bin(x) for x in self.goodrevs]
45 45 if os.path.exists(os.path.join(self.path, self.bad_path)):
46 46 r = self.opener(self.bad_path).read().splitlines()
47 47 if r:
48 48 self.badrev = hg.bin(r.pop(0))
49 49
50 50 def __del__(self):
51 51 if not os.path.isdir(self.path):
52 52 return
53 53 f = self.opener(self.good_path, "w")
54 54 f.write("\n".join([hg.hex(r) for r in self.goodrevs]))
55 55 if len(self.goodrevs) > 0:
56 56 f.write("\n")
57 57 f = self.opener(self.bad_path, "w")
58 58 if self.badrev:
59 59 f.write(hg.hex(self.badrev) + "\n")
60 60
61 61 def init(self):
62 62 """start a new bisection"""
63 63 if os.path.isdir(self.path):
64 64 self.ui.warn("bisect directory already exists\n")
65 65 return 1
66 66 os.mkdir(self.path)
67 67 check_clean(self.ui, self.repo)
68 68 return 0
69 69
70 70 def reset(self):
71 71 """finish a bisection"""
72 72 if os.path.isdir(self.path):
73 73 sl = [os.path.join(self.path, p)
74 74 for p in [self.bad_path, self.good_path]]
75 75 for s in sl:
76 76 if os.path.exists(s):
77 77 os.unlink(s)
78 78 os.rmdir(self.path)
79 79 # Not sure about this
80 80 #self.ui.write("Going back to tip\n")
81 81 #self.repo.update(self.repo.changelog.tip())
82 82 return 1
83 83
84 84 def num_ancestors(self, head=None, stop=None):
85 85 """
86 86 returns a dict with the mapping:
87 87 node -> number of ancestors (self included)
88 88 for all nodes who are ancestor of head and
89 89 not in stop.
90 90 """
91 91 if head is None:
92 92 head = self.badrev
93 93 return self.__ancestors_and_nb_ancestors(head, stop)[1]
94 94
95 95 def ancestors(self, head=None, stop=None):
96 96 """
97 97 returns the set of the ancestors of head (self included)
98 98 who are not in stop.
99 99 """
100 100 if head is None:
101 101 head = self.badrev
102 102 return self.__ancestors_and_nb_ancestors(head, stop)[0]
103 103
104 104 def __ancestors_and_nb_ancestors(self, head, stop=None):
105 105 """
106 106 if stop is None then ancestors of goodrevs are used as
107 107 lower limit.
108 108
109 109 returns (anc, n_child) where anc is the set of the ancestors of head
110 110 and n_child is a dictionary with the following mapping:
111 111 node -> number of ancestors (self included)
112 112 """
113 113 cl = self.repo.changelog
114 114 if not stop:
115 115 stop = sets.Set([])
116 for g in reversed(self.goodrevs):
116 for i in xrange(len(self.goodrevs)-1, -1, -1):
117 g = self.goodrevs[i]
117 118 if g in stop:
118 119 continue
119 120 stop.update(cl.reachable(g))
120 121 def num_children(a):
121 122 """
122 123 returns a dictionnary with the following mapping
123 124 node -> [number of children, empty set]
124 125 """
125 126 d = {a: [0, sets.Set([])]}
126 127 for i in xrange(cl.rev(a)+1):
127 128 n = cl.node(i)
128 129 if not d.has_key(n):
129 130 d[n] = [0, sets.Set([])]
130 131 parents = [p for p in cl.parents(n) if p != hg.nullid]
131 132 for p in parents:
132 133 d[p][0] += 1
133 134 return d
134 135
135 136 if head in stop:
136 137 self.ui.warn("Unconsistent state, %s is good and bad\n"
137 138 % hg.hex(head))
138 139 sys.exit(1)
139 140 n_child = num_children(head)
140 141 for i in xrange(cl.rev(head)+1):
141 142 n = cl.node(i)
142 143 parents = [p for p in cl.parents(n) if p != hg.nullid]
143 144 for p in parents:
144 145 n_child[p][0] -= 1
145 146 if not n in stop:
146 147 n_child[n][1].union_update(n_child[p][1])
147 148 if n_child[p][0] == 0:
148 149 n_child[p] = len(n_child[p][1])
149 150 if not n in stop:
150 151 n_child[n][1].add(n)
151 152 if n_child[n][0] == 0:
152 153 if n == head:
153 154 anc = n_child[n][1]
154 155 n_child[n] = len(n_child[n][1])
155 156 return anc, n_child
156 157
157 158 def next(self):
158 159 if not self.badrev:
159 160 self.ui.warn("You should give at least one bad\n")
160 161 sys.exit(1)
161 162 if not self.goodrevs:
162 163 self.ui.warn("No good revision given\n")
163 164 self.ui.warn("Assuming the first revision is good\n")
164 165 ancestors, num_ancestors = self.__ancestors_and_nb_ancestors(
165 166 self.badrev)
166 167 tot = len(ancestors)
167 168 if tot == 1:
168 169 if ancestors.pop() != self.badrev:
169 170 self.ui.warn("Could not find the first bad revision\n")
170 171 sys.exit(1)
171 172 self.ui.write(
172 173 "The first bad revision is : %s\n" % hg.hex(self.badrev))
173 174 sys.exit(0)
174 175 self.ui.write("%d revisions left\n" % tot)
175 176 best_rev = None
176 177 best_len = -1
177 178 for n in ancestors:
178 179 l = num_ancestors[n]
179 180 l = min(l, tot - l)
180 181 if l > best_len:
181 182 best_len = l
182 183 best_rev = n
183 184 return best_rev
184 185
185 186 def autonext(self):
186 187 """find and update to the next revision to test"""
187 188 check_clean(self.ui, self.repo)
188 189 rev = self.next()
189 190 self.ui.write("Now testing %s\n" % hg.hex(rev))
190 191 return self.repo.update(rev, force=True)
191 192
192 193 def good(self, rev):
193 194 self.goodrevs.append(rev)
194 195
195 196 def autogood(self, rev=None):
196 197 """mark revision as good and update to the next revision to test"""
197 198 check_clean(self.ui, self.repo)
198 199 rev = lookup_rev(self.ui, self.repo, rev)
199 200 self.good(rev)
200 201 if self.badrev:
201 202 self.autonext()
202 203
203 204 def bad(self, rev):
204 205 self.badrev = rev
205 206
206 207 def autobad(self, rev=None):
207 208 """mark revision as bad and update to the next revision to test"""
208 209 check_clean(self.ui, self.repo)
209 210 rev = lookup_rev(self.ui, self.repo, rev)
210 211 self.bad(rev)
211 212 if self.goodrevs:
212 213 self.autonext()
213 214
214 215 # should we put it in the class ?
215 216 def test(ui, repo, rev):
216 217 """test the bisection code"""
217 218 b = bisect(ui, repo)
218 219 rev = repo.lookup(rev)
219 220 ui.write("testing with rev %s\n" % hg.hex(rev))
220 221 anc = b.ancestors()
221 222 while len(anc) > 1:
222 223 if not rev in anc:
223 224 ui.warn("failure while bisecting\n")
224 225 sys.exit(1)
225 226 ui.write("it worked :)\n")
226 227 new_rev = b.next()
227 228 ui.write("choosing if good or bad\n")
228 229 if rev in b.ancestors(head=new_rev):
229 230 b.bad(new_rev)
230 231 ui.write("it is bad\n")
231 232 else:
232 233 b.good(new_rev)
233 234 ui.write("it is good\n")
234 235 anc = b.ancestors()
235 236 repo.update(new_rev, force=True)
236 237 for v in anc:
237 238 if v != rev:
238 239 ui.warn("fail to found cset! :(\n")
239 240 return 1
240 241 ui.write("Found bad cset: %s\n" % hg.hex(b.badrev))
241 242 ui.write("Everything is ok :)\n")
242 243 return 0
243 244
244 245 def bisect_run(ui, repo, cmd=None, *args):
245 246 """bisect extension: dichotomic search in the DAG of changesets
246 247 for subcommands see "hg bisect help\"
247 248 """
248 249 def help_(cmd=None, *args):
249 250 """show help for a given bisect subcommand or all subcommands"""
250 251 cmdtable = bisectcmdtable
251 252 if cmd:
252 253 doc = cmdtable[cmd][0].__doc__
253 254 synopsis = cmdtable[cmd][2]
254 255 ui.write(synopsis + "\n")
255 256 ui.write("\n" + doc + "\n")
256 257 return
257 258 ui.write("list of subcommands for the bisect extension\n\n")
258 259 cmds = cmdtable.keys()
259 260 cmds.sort()
260 261 m = max([len(c) for c in cmds])
261 262 for cmd in cmds:
262 263 doc = cmdtable[cmd][0].__doc__.splitlines(0)[0].rstrip()
263 264 ui.write(" %-*s %s\n" % (m, cmd, doc))
264 265
265 266 b = bisect(ui, repo)
266 267 bisectcmdtable = {
267 268 "init": (b.init, 0, "hg bisect init"),
268 269 "bad": (b.autobad, 1, "hg bisect bad [<rev>]"),
269 270 "good": (b.autogood, 1, "hg bisect good [<rev>]"),
270 271 "next": (b.autonext, 0, "hg bisect next"),
271 272 "reset": (b.reset, 0, "hg bisect reset"),
272 273 "help": (help_, 1, "hg bisect help [<subcommand>]"),
273 274 }
274 275
275 276 if not bisectcmdtable.has_key(cmd):
276 277 ui.warn("bisect: Unknown sub-command\n")
277 278 return help_()
278 279 if len(args) > bisectcmdtable[cmd][1]:
279 280 ui.warn("bisect: Too many arguments\n")
280 281 return help_()
281 282 return bisectcmdtable[cmd][0](*args)
282 283
283 284 cmdtable = {
284 285 "bisect": (bisect_run, [], "hg bisect [help|init|reset|next|good|bad]"),
285 286 #"bisect-test": (test, [], "hg bisect-test rev"),
286 287 }
General Comments 0
You need to be logged in to leave comments. Login now