Mercurial > hg
comparison tests/drawdag.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | 15f04d652b62 |
children | dda2341d6664 |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
100 | 100 |
101 cmdtable = {} | 101 cmdtable = {} |
102 command = registrar.command(cmdtable) | 102 command = registrar.command(cmdtable) |
103 | 103 |
104 _pipechars = b'\\/+-|' | 104 _pipechars = b'\\/+-|' |
105 _nonpipechars = b''.join(pycompat.bytechr(i) for i in range(33, 127) | 105 _nonpipechars = b''.join( |
106 if pycompat.bytechr(i) not in _pipechars) | 106 pycompat.bytechr(i) |
107 for i in range(33, 127) | |
108 if pycompat.bytechr(i) not in _pipechars | |
109 ) | |
110 | |
107 | 111 |
108 def _isname(ch): | 112 def _isname(ch): |
109 """char -> bool. return True if ch looks like part of a name, False | 113 """char -> bool. return True if ch looks like part of a name, False |
110 otherwise""" | 114 otherwise""" |
111 return ch in _nonpipechars | 115 return ch in _nonpipechars |
116 | |
112 | 117 |
113 def _parseasciigraph(text): | 118 def _parseasciigraph(text): |
114 r"""str -> {str : [str]}. convert the ASCII graph to edges | 119 r"""str -> {str : [str]}. convert the ASCII graph to edges |
115 | 120 |
116 >>> import pprint | 121 >>> import pprint |
164 """(int, int) -> char. give a coordinate, return the char. return a | 169 """(int, int) -> char. give a coordinate, return the char. return a |
165 space for anything out of range""" | 170 space for anything out of range""" |
166 if x < 0 or y < 0: | 171 if x < 0 or y < 0: |
167 return b' ' | 172 return b' ' |
168 try: | 173 try: |
169 return lines[y][x:x + 1] or b' ' | 174 return lines[y][x : x + 1] or b' ' |
170 except IndexError: | 175 except IndexError: |
171 return b' ' | 176 return b' ' |
172 | 177 |
173 def getname(y, x): | 178 def getname(y, x): |
174 """(int, int) -> str. like get(y, x) but concatenate left and right | 179 """(int, int) -> str. like get(y, x) but concatenate left and right |
259 if _isname(ch): | 264 if _isname(ch): |
260 edges[getname(y, x)] += parents(y, x) | 265 edges[getname(y, x)] += parents(y, x) |
261 | 266 |
262 return dict(edges) | 267 return dict(edges) |
263 | 268 |
269 | |
264 class simplefilectx(object): | 270 class simplefilectx(object): |
265 def __init__(self, path, data): | 271 def __init__(self, path, data): |
266 self._data = data | 272 self._data = data |
267 self._path = path | 273 self._path = path |
268 | 274 |
278 def copysource(self): | 284 def copysource(self): |
279 return None | 285 return None |
280 | 286 |
281 def flags(self): | 287 def flags(self): |
282 return b'' | 288 return b'' |
289 | |
283 | 290 |
284 class simplecommitctx(context.committablectx): | 291 class simplecommitctx(context.committablectx): |
285 def __init__(self, repo, name, parentctxs, added): | 292 def __init__(self, repo, name, parentctxs, added): |
286 opts = { | 293 opts = { |
287 'changes': scmutil.status([], list(added), [], [], [], [], []), | 294 'changes': scmutil.status([], list(added), [], [], [], [], []), |
303 def p1copies(self): | 310 def p1copies(self): |
304 return {} | 311 return {} |
305 | 312 |
306 def p2copies(self): | 313 def p2copies(self): |
307 return {} | 314 return {} |
315 | |
308 | 316 |
309 def _walkgraph(edges): | 317 def _walkgraph(edges): |
310 """yield node, parents in topologically order""" | 318 """yield node, parents in topologically order""" |
311 visible = set(edges.keys()) | 319 visible = set(edges.keys()) |
312 remaining = {} # {str: [str]} | 320 remaining = {} # {str: [str]} |
325 del remaining[leaf] | 333 del remaining[leaf] |
326 for k, v in remaining.items(): | 334 for k, v in remaining.items(): |
327 if leaf in v: | 335 if leaf in v: |
328 v.remove(leaf) | 336 v.remove(leaf) |
329 | 337 |
338 | |
330 def _getcomments(text): | 339 def _getcomments(text): |
331 r""" | 340 r""" |
332 >>> [pycompat.sysstr(s) for s in _getcomments(br''' | 341 >>> [pycompat.sysstr(s) for s in _getcomments(br''' |
333 ... G | 342 ... G |
334 ... | | 343 ... | |
343 for line in text.splitlines(): | 352 for line in text.splitlines(): |
344 if b' # ' not in line: | 353 if b' # ' not in line: |
345 continue | 354 continue |
346 yield line.split(b' # ', 1)[1].split(b' # ')[0].strip() | 355 yield line.split(b' # ', 1)[1].split(b' # ')[0].strip() |
347 | 356 |
357 | |
348 @command(b'debugdrawdag', []) | 358 @command(b'debugdrawdag', []) |
349 def debugdrawdag(ui, repo, **opts): | 359 def debugdrawdag(ui, repo, **opts): |
350 r"""read an ASCII graph from stdin and create changesets | 360 r"""read an ASCII graph from stdin and create changesets |
351 | 361 |
352 The ASCII graph is like what :hg:`log -G` outputs, with each `o` replaced | 362 The ASCII graph is like what :hg:`log -G` outputs, with each `o` replaced |
366 | 376 |
367 # parse the graph and make sure len(parents) <= 2 for each node | 377 # parse the graph and make sure len(parents) <= 2 for each node |
368 edges = _parseasciigraph(text) | 378 edges = _parseasciigraph(text) |
369 for k, v in edges.items(): | 379 for k, v in edges.items(): |
370 if len(v) > 2: | 380 if len(v) > 2: |
371 raise error.Abort(_('%s: too many parents: %s') | 381 raise error.Abort(_('%s: too many parents: %s') % (k, b' '.join(v))) |
372 % (k, b' '.join(v))) | |
373 | 382 |
374 # parse comments to get extra file content instructions | 383 # parse comments to get extra file content instructions |
375 files = collections.defaultdict(dict) # {(name, path): content} | 384 files = collections.defaultdict(dict) # {(name, path): content} |
376 comments = list(_getcomments(text)) | 385 comments = list(_getcomments(text)) |
377 filere = re.compile(br'^(\w+)/([\w/]+)\s*=\s*(.*)$', re.M) | 386 filere = re.compile(br'^(\w+)/([\w/]+)\s*=\s*(.*)$', re.M) |
378 for name, path, content in filere.findall(b'\n'.join(comments)): | 387 for name, path, content in filere.findall(b'\n'.join(comments)): |
379 content = content.replace(br'\n', b'\n').replace(br'\1', b'\1') | 388 content = content.replace(br'\n', b'\n').replace(br'\1', b'\1') |
380 files[name][path] = content | 389 files[name][path] = content |
408 for path, content in files.get(name, {}).items(): | 417 for path, content in files.get(name, {}).items(): |
409 added[path] = content | 418 added[path] = content |
410 ctx = simplecommitctx(repo, name, pctxs, added) | 419 ctx = simplecommitctx(repo, name, pctxs, added) |
411 n = ctx.commit() | 420 n = ctx.commit() |
412 committed[name] = n | 421 committed[name] = n |
413 tagsmod.tag(repo, [name], n, message=None, user=None, date=None, | 422 tagsmod.tag( |
414 local=True) | 423 repo, [name], n, message=None, user=None, date=None, local=True |
424 ) | |
415 | 425 |
416 # handle special comments | 426 # handle special comments |
417 with repo.wlock(), repo.lock(), repo.transaction(b'drawdag'): | 427 with repo.wlock(), repo.lock(), repo.transaction(b'drawdag'): |
418 getctx = lambda x: repo.unfiltered()[committed[x.strip()]] | 428 getctx = lambda x: repo.unfiltered()[committed[x.strip()]] |
419 for comment in comments: | 429 for comment in comments: |
420 rels = [] # obsolete relationships | 430 rels = [] # obsolete relationships |
421 args = comment.split(b':', 1) | 431 args = comment.split(b':', 1) |
422 if len(args) <= 1: | 432 if len(args) <= 1: |
423 continue | 433 continue |
424 | 434 |
425 cmd = args[0].strip() | 435 cmd = args[0].strip() |