| 1 |
# drawdag.py - convert ASCII revision DAG to actual changesets |
| 2 |
# |
| 3 |
# Copyright 2016 Facebook, Inc. |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
""" |
| 8 |
create changesets from an ASCII graph for testing purpose. |
| 9 |
|
| 10 |
For example, given the following input:: |
| 11 |
|
| 12 |
c d |
| 13 |
|/ |
| 14 |
b |
| 15 |
| |
| 16 |
a |
| 17 |
|
| 18 |
4 changesets and 4 local tags will be created. |
| 19 |
`hg log -G -T "{rev} {desc} (tag: {tags})"` will output:: |
| 20 |
|
| 21 |
o 3 d (tag: d tip) |
| 22 |
| |
| 23 |
| o 2 c (tag: c) |
| 24 |
|/ |
| 25 |
o 1 b (tag: b) |
| 26 |
| |
| 27 |
o 0 a (tag: a) |
| 28 |
|
| 29 |
For root nodes (nodes without parents) in the graph, they can be revsets |
| 30 |
pointing to existing nodes. The ASCII graph could also have disconnected |
| 31 |
components with same names referring to the same changeset. |
| 32 |
|
| 33 |
Therefore, given the repo having the 4 changesets (and tags) above, with the |
| 34 |
following ASCII graph as input:: |
| 35 |
|
| 36 |
foo bar bar foo |
| 37 |
| / | | |
| 38 |
ancestor(c,d) a baz |
| 39 |
|
| 40 |
The result (`hg log -G -T "{desc}"`) will look like:: |
| 41 |
|
| 42 |
o foo |
| 43 |
|\ |
| 44 |
+---o bar |
| 45 |
| | | |
| 46 |
| o | baz |
| 47 |
| / |
| 48 |
+---o d |
| 49 |
| | |
| 50 |
+---o c |
| 51 |
| | |
| 52 |
o | b |
| 53 |
|/ |
| 54 |
o a |
| 55 |
|
| 56 |
Note that if you take the above `hg log` output directly as input. It will work |
| 57 |
as expected - the result would be an isomorphic graph:: |
| 58 |
|
| 59 |
o foo |
| 60 |
|\ |
| 61 |
| | o d |
| 62 |
| |/ |
| 63 |
| | o c |
| 64 |
| |/ |
| 65 |
| | o bar |
| 66 |
| |/| |
| 67 |
| o | b |
| 68 |
| |/ |
| 69 |
o / baz |
| 70 |
/ |
| 71 |
o a |
| 72 |
|
| 73 |
This is because 'o' is specially handled in the input: instead of using 'o' as |
| 74 |
the node name, the word to the right will be used. |
| 75 |
|
| 76 |
Some special comments could have side effects: |
| 77 |
|
| 78 |
- Create obsmarkers |
| 79 |
# replace: A -> B -> C -> D # chained 1 to 1 replacements |
| 80 |
# split: A -> B, C # 1 to many |
| 81 |
# prune: A, B, C # many to nothing |
| 82 |
|
| 83 |
Special comment can also be used to control file content for some revision. |
| 84 |
|
| 85 |
The example below create two extra files in A, update on in B and delete the |
| 86 |
other on in C (using a special content to mark the deletion). |
| 87 |
|
| 88 |
In all case the file matching the node name is created for all non-merge |
| 89 |
commit. |
| 90 |
|
| 91 |
C # A/file/path=content in A |
| 92 |
| # A/other/file/path=content for another file |
| 93 |
B # B/file/path=updated content in B |
| 94 |
| # C/other/file/path=<deleted> |
| 95 |
A |
| 96 |
""" |
| 97 |
|
| 98 |
import collections |
| 99 |
import itertools |
| 100 |
import re |
| 101 |
|
| 102 |
from mercurial.i18n import _ |
| 103 |
from mercurial import ( |
| 104 |
context, |
| 105 |
error, |
| 106 |
obsolete, |
| 107 |
pycompat, |
| 108 |
registrar, |
| 109 |
scmutil, |
| 110 |
tags as tagsmod, |
| 111 |
) |
| 112 |
|
| 113 |
cmdtable = {} |
| 114 |
command = registrar.command(cmdtable) |
| 115 |
|
| 116 |
_pipechars = b'\\/+-|' |
| 117 |
_nonpipechars = b''.join( |
| 118 |
pycompat.bytechr(i) |
| 119 |
for i in range(33, 127) |
| 120 |
if pycompat.bytechr(i) not in _pipechars |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
def _isname(ch): |
| 125 |
"""char -> bool. return True if ch looks like part of a name, False |
| 126 |
otherwise""" |
| 127 |
return ch in _nonpipechars |
| 128 |
|
| 129 |
|
| 130 |
def _parseasciigraph(text): |
| 131 |
r"""str -> {str : [str]}. convert the ASCII graph to edges |
| 132 |
|
| 133 |
>>> import pprint |
| 134 |
>>> pprint.pprint({pycompat.sysstr(k): [pycompat.sysstr(vv) for vv in v] |
| 135 |
... for k, v in _parseasciigraph(br''' |
| 136 |
... G |
| 137 |
... | |
| 138 |
... I D C F # split: B -> E, F, G |
| 139 |
... \ \| | # replace: C -> D -> H |
| 140 |
... H B E # prune: F, I |
| 141 |
... \|/ |
| 142 |
... A |
| 143 |
... ''').items()}) |
| 144 |
{'A': [], |
| 145 |
'B': ['A'], |
| 146 |
'C': ['B'], |
| 147 |
'D': ['B'], |
| 148 |
'E': ['A'], |
| 149 |
'F': ['E'], |
| 150 |
'G': ['F'], |
| 151 |
'H': ['A'], |
| 152 |
'I': ['H']} |
| 153 |
>>> pprint.pprint({pycompat.sysstr(k): [pycompat.sysstr(vv) for vv in v] |
| 154 |
... for k, v in _parseasciigraph(br''' |
| 155 |
... o foo |
| 156 |
... |\ |
| 157 |
... +---o bar |
| 158 |
... | | | |
| 159 |
... | o | baz |
| 160 |
... | / |
| 161 |
... +---o d |
| 162 |
... | | |
| 163 |
... +---o c |
| 164 |
... | | |
| 165 |
... o | b |
| 166 |
... |/ |
| 167 |
... o a |
| 168 |
... ''').items()}) |
| 169 |
{'a': [], |
| 170 |
'b': ['a'], |
| 171 |
'bar': ['b', 'a'], |
| 172 |
'baz': [], |
| 173 |
'c': ['b'], |
| 174 |
'd': ['b'], |
| 175 |
'foo': ['baz', 'b']} |
| 176 |
""" |
| 177 |
lines = text.splitlines() |
| 178 |
edges = collections.defaultdict(list) # {node: []} |
| 179 |
|
| 180 |
def get(y, x): |
| 181 |
"""(int, int) -> char. give a coordinate, return the char. return a |
| 182 |
space for anything out of range""" |
| 183 |
if x < 0 or y < 0: |
| 184 |
return b' ' |
| 185 |
try: |
| 186 |
return lines[y][x : x + 1] or b' ' |
| 187 |
except IndexError: |
| 188 |
return b' ' |
| 189 |
|
| 190 |
def getname(y, x): |
| 191 |
"""(int, int) -> str. like get(y, x) but concatenate left and right |
| 192 |
parts. if name is an 'o', try to replace it to the right""" |
| 193 |
result = b'' |
| 194 |
for i in itertools.count(0): |
| 195 |
ch = get(y, x - i) |
| 196 |
if not _isname(ch): |
| 197 |
break |
| 198 |
result = ch + result |
| 199 |
for i in itertools.count(1): |
| 200 |
ch = get(y, x + i) |
| 201 |
if not _isname(ch): |
| 202 |
break |
| 203 |
result += ch |
| 204 |
if result == b'o': |
| 205 |
# special handling, find the name to the right |
| 206 |
result = b'' |
| 207 |
for i in itertools.count(2): |
| 208 |
ch = get(y, x + i) |
| 209 |
if ch == b' ' or ch in _pipechars: |
| 210 |
if result or x + i >= len(lines[y]): |
| 211 |
break |
| 212 |
else: |
| 213 |
result += ch |
| 214 |
return result or b'o' |
| 215 |
return result |
| 216 |
|
| 217 |
def parents(y, x): |
| 218 |
"""(int, int) -> [str]. follow the ASCII edges at given position, |
| 219 |
return a list of parents""" |
| 220 |
visited = {(y, x)} |
| 221 |
visit = [] |
| 222 |
result = [] |
| 223 |
|
| 224 |
def follow(y, x, expected): |
| 225 |
"""conditionally append (y, x) to visit array, if it's a char |
| 226 |
in excepted. 'o' in expected means an '_isname' test. |
| 227 |
if '-' (or '+') is not in excepted, and get(y, x) is '-' (or '+'), |
| 228 |
the next line (y + 1, x) will be checked instead.""" |
| 229 |
ch = get(y, x) |
| 230 |
if any(ch == c and c not in expected for c in (b'-', b'+')): |
| 231 |
y += 1 |
| 232 |
return follow(y + 1, x, expected) |
| 233 |
if ch in expected or (b'o' in expected and _isname(ch)): |
| 234 |
visit.append((y, x)) |
| 235 |
|
| 236 |
# -o- # starting point: |
| 237 |
# /|\ # follow '-' (horizontally), and '/|\' (to the bottom) |
| 238 |
follow(y + 1, x, b'|') |
| 239 |
follow(y + 1, x - 1, b'/') |
| 240 |
follow(y + 1, x + 1, b'\\') |
| 241 |
follow(y, x - 1, b'-') |
| 242 |
follow(y, x + 1, b'-') |
| 243 |
|
| 244 |
while visit: |
| 245 |
y, x = visit.pop() |
| 246 |
if (y, x) in visited: |
| 247 |
continue |
| 248 |
visited.add((y, x)) |
| 249 |
ch = get(y, x) |
| 250 |
if _isname(ch): |
| 251 |
result.append(getname(y, x)) |
| 252 |
continue |
| 253 |
elif ch == b'|': |
| 254 |
follow(y + 1, x, b'/|o') |
| 255 |
follow(y + 1, x - 1, b'/') |
| 256 |
follow(y + 1, x + 1, b'\\') |
| 257 |
elif ch == b'+': |
| 258 |
follow(y, x - 1, b'-') |
| 259 |
follow(y, x + 1, b'-') |
| 260 |
follow(y + 1, x - 1, b'/') |
| 261 |
follow(y + 1, x + 1, b'\\') |
| 262 |
follow(y + 1, x, b'|') |
| 263 |
elif ch == b'\\': |
| 264 |
follow(y + 1, x + 1, b'\\|o') |
| 265 |
elif ch == b'/': |
| 266 |
follow(y + 1, x - 1, b'/|o') |
| 267 |
elif ch == b'-': |
| 268 |
follow(y, x - 1, b'-+o') |
| 269 |
follow(y, x + 1, b'-+o') |
| 270 |
return result |
| 271 |
|
| 272 |
for y, line in enumerate(lines): |
| 273 |
for x, ch in enumerate(pycompat.bytestr(line)): |
| 274 |
if ch == b'#': # comment |
| 275 |
break |
| 276 |
if _isname(ch): |
| 277 |
edges[getname(y, x)] += parents(y, x) |
| 278 |
|
| 279 |
return dict(edges) |
| 280 |
|
| 281 |
|
| 282 |
class simplefilectx: |
| 283 |
def __init__(self, path, data): |
| 284 |
self._data = data |
| 285 |
self._path = path |
| 286 |
|
| 287 |
def data(self): |
| 288 |
return self._data |
| 289 |
|
| 290 |
def filenode(self): |
| 291 |
return None |
| 292 |
|
| 293 |
def path(self): |
| 294 |
return self._path |
| 295 |
|
| 296 |
def copysource(self): |
| 297 |
return None |
| 298 |
|
| 299 |
def flags(self): |
| 300 |
return b'' |
| 301 |
|
| 302 |
|
| 303 |
class simplecommitctx(context.committablectx): |
| 304 |
def __init__(self, repo, name, parentctxs, added): |
| 305 |
opts = { |
| 306 |
'changes': scmutil.status([], list(added), [], [], [], [], []), |
| 307 |
'date': b'0 0', |
| 308 |
'extra': {b'branch': b'default'}, |
| 309 |
} |
| 310 |
super().__init__(repo, name, **opts) |
| 311 |
self._added = added |
| 312 |
self._parents = parentctxs |
| 313 |
while len(self._parents) < 2: |
| 314 |
self._parents.append(repo[repo.nullid]) |
| 315 |
|
| 316 |
def filectx(self, key): |
| 317 |
content = self._added.get(key) |
| 318 |
if content is not None: |
| 319 |
return simplefilectx(key, content) |
| 320 |
return None |
| 321 |
|
| 322 |
def commit(self): |
| 323 |
return self._repo.commitctx(self) |
| 324 |
|
| 325 |
def p1copies(self): |
| 326 |
return {} |
| 327 |
|
| 328 |
def p2copies(self): |
| 329 |
return {} |
| 330 |
|
| 331 |
|
| 332 |
def _walkgraph(edges): |
| 333 |
"""yield node, parents in topologically order""" |
| 334 |
visible = set(edges.keys()) |
| 335 |
remaining = {} # {str: [str]} |
| 336 |
for k, vs in edges.items(): |
| 337 |
for v in vs: |
| 338 |
if v not in remaining: |
| 339 |
remaining[v] = [] |
| 340 |
remaining[k] = vs[:] |
| 341 |
while remaining: |
| 342 |
leafs = [k for k, v in remaining.items() if not v] |
| 343 |
if not leafs: |
| 344 |
raise error.Abort(_('the graph has cycles')) |
| 345 |
for leaf in sorted(leafs): |
| 346 |
if leaf in visible: |
| 347 |
yield leaf, edges[leaf] |
| 348 |
del remaining[leaf] |
| 349 |
for k, v in remaining.items(): |
| 350 |
if leaf in v: |
| 351 |
v.remove(leaf) |
| 352 |
|
| 353 |
|
| 354 |
def _getcomments(text): |
| 355 |
r""" |
| 356 |
>>> [pycompat.sysstr(s) for s in _getcomments(br''' |
| 357 |
... G |
| 358 |
... | |
| 359 |
... I D C F # split: B -> E, F, G |
| 360 |
... \ \| | # replace: C -> D -> H |
| 361 |
... H B E # prune: F, I |
| 362 |
... \|/ |
| 363 |
... A |
| 364 |
... ''')] |
| 365 |
['split: B -> E, F, G', 'replace: C -> D -> H', 'prune: F, I'] |
| 366 |
""" |
| 367 |
for line in text.splitlines(): |
| 368 |
if b' # ' not in line: |
| 369 |
continue |
| 370 |
yield line.split(b' # ', 1)[1].split(b' # ')[0].strip() |
| 371 |
|
| 372 |
|
| 373 |
@command(b'debugdrawdag', []) |
| 374 |
def debugdrawdag(ui, repo, **opts): |
| 375 |
r"""read an ASCII graph from stdin and create changesets |
| 376 |
|
| 377 |
The ASCII graph is like what :hg:`log -G` outputs, with each `o` replaced |
| 378 |
to the name of the node. The command will create dummy changesets and local |
| 379 |
tags with those names to make the dummy changesets easier to be referred |
| 380 |
to. |
| 381 |
|
| 382 |
If the name of a node is a single character 'o', It will be replaced by the |
| 383 |
word to the right. This makes it easier to reuse |
| 384 |
:hg:`log -G -T '{desc}'` outputs. |
| 385 |
|
| 386 |
For root (no parents) nodes, revset can be used to query existing repo. |
| 387 |
Note that the revset cannot have confusing characters which can be seen as |
| 388 |
the part of the graph edges, like `|/+-\`. |
| 389 |
""" |
| 390 |
text = ui.fin.read() |
| 391 |
|
| 392 |
# parse the graph and make sure len(parents) <= 2 for each node |
| 393 |
edges = _parseasciigraph(text) |
| 394 |
for k, v in edges.items(): |
| 395 |
if len(v) > 2: |
| 396 |
raise error.Abort(_('%s: too many parents: %s') % (k, b' '.join(v))) |
| 397 |
|
| 398 |
# parse comments to get extra file content instructions |
| 399 |
files = collections.defaultdict(dict) # {(name, path): content} |
| 400 |
comments = list(_getcomments(text)) |
| 401 |
filere = re.compile(br'^(\w+)/([-_\w/"]+)\s*=\s*(.*)$', re.M) |
| 402 |
for name, path, content in filere.findall(b'\n'.join(comments)): |
| 403 |
content = content.replace(br'\n', b'\n').replace(br'\1', b'\1') |
| 404 |
if content == b"<deleted>": |
| 405 |
files[name][path] = None |
| 406 |
else: |
| 407 |
files[name][path] = content |
| 408 |
|
| 409 |
committed = {None: repo.nullid} # {name: node} |
| 410 |
|
| 411 |
# for leaf nodes, try to find existing nodes in repo |
| 412 |
for name, parents in edges.items(): |
| 413 |
if len(parents) == 0: |
| 414 |
try: |
| 415 |
committed[name] = scmutil.revsingle(repo, name) |
| 416 |
except error.RepoLookupError: |
| 417 |
pass |
| 418 |
|
| 419 |
# commit in topological order |
| 420 |
for name, parents in _walkgraph(edges): |
| 421 |
if name in committed: |
| 422 |
continue |
| 423 |
pctxs = [repo[committed[n]] for n in parents] |
| 424 |
pctxs.sort(key=lambda c: c.node()) |
| 425 |
added = {} |
| 426 |
if len(parents) > 1: |
| 427 |
# If it's a merge, take the files and contents from the parents |
| 428 |
for f in pctxs[1].manifest(): |
| 429 |
if f not in pctxs[0].manifest(): |
| 430 |
added[f] = pctxs[1][f].data() |
| 431 |
else: |
| 432 |
# If it's not a merge, add a single file |
| 433 |
added[name] = name |
| 434 |
# add extra file contents in comments |
| 435 |
for path, content in files.get(name, {}).items(): |
| 436 |
added[path] = content |
| 437 |
ctx = simplecommitctx(repo, name, pctxs, added) |
| 438 |
n = ctx.commit() |
| 439 |
committed[name] = n |
| 440 |
tagsmod.tag( |
| 441 |
repo, [name], n, message=None, user=None, date=None, local=True |
| 442 |
) |
| 443 |
|
| 444 |
# handle special comments |
| 445 |
with repo.wlock(), repo.lock(), repo.transaction(b'drawdag'): |
| 446 |
getctx = lambda x: repo.unfiltered()[committed[x.strip()]] |
| 447 |
for comment in comments: |
| 448 |
rels = [] # obsolete relationships |
| 449 |
args = comment.split(b':', 1) |
| 450 |
if len(args) <= 1: |
| 451 |
continue |
| 452 |
|
| 453 |
cmd = args[0].strip() |
| 454 |
arg = args[1].strip() |
| 455 |
|
| 456 |
if cmd in (b'replace', b'rebase', b'amend'): |
| 457 |
nodes = [getctx(m) for m in arg.split(b'->')] |
| 458 |
for i in range(len(nodes) - 1): |
| 459 |
rels.append(((nodes[i],), (nodes[i + 1],))) |
| 460 |
elif cmd in (b'split',): |
| 461 |
pre, succs = arg.split(b'->') |
| 462 |
succs = succs.split(b',') |
| 463 |
rels.append(((getctx(pre),), (getctx(s) for s in succs))) |
| 464 |
elif cmd in (b'prune',): |
| 465 |
for n in arg.split(b','): |
| 466 |
rels.append(((getctx(n),), ())) |
| 467 |
if rels: |
| 468 |
obsolete.createmarkers(repo, rels, date=(0, 0), operation=cmd) |