在这里,有我的脚本:
#!/usr/bin/env python
from itertools import chain
import hglib
#from pygraphviz import *
repo = hglib.open('.')
log = dict((t[0], t) for t in repo.log())
def graph(): return dict((v, []) for v in log)
forward, backward, jump = (graph() for i in range(3))
for target in log:
for parent in repo.parents(target) or []:
source = parent[0]
forward[source].append(target)
backward[target].append(source)
def endpoint(v):
if len(forward[v]) != 1: return True
if len(backward[forward[v][0]]) != 1: return True
if len(backward[v]) != 1: return True
if len(forward[backward[v][0]]) != 1: return True
for v in forward:
if endpoint(v):
w = v
while len(forward[w]) == 1 == len(backward[forward[w][0]]):
w = forward[w][0]
jump[v] = w
else: del jump[v]
def vertex(tupl): return 'v' + tupl[1][:5]
print 'digraph {'
colors = ['red', 'green', 'blue', 'yellow', 'cyan', 'magenta',
'orange', 'chartreuse']
authors = dict()
for v in sorted(forward, key=int):
if not endpoint(v) or v not in jump: continue
node = 'v%s' % v
if jump[v] != v:
sep = '+' if forward[v] == jump[v] else '::'
label = '%s%s%s' % (v, sep, jump[v])
assert int(jump[v]) > int(v)
v = jump[v]
del jump[v]
else:
label = v
author = log[v][4]
print '// %s' % author
if author not in authors: authors[author] = colors[len(authors) %
len(colors)]
attrs = dict(color=authors[author], label=label, style='bold').items()
print '\t%s [%s]' % (node, ','.join('%s="%s"' % kv for kv in attrs))
for w in forward[v]: print '\t%s -> v%s' % (node, w)
print '}'
如您所知,我使用 hglib 来获取 Mercurial 数据(hglib.open('.').log()
是一个元组列表,每次提交一个)。我使用 graphviz 进行可视化。没有图书馆,我只是*打印*该死的东西;-)
我像这样运行脚本加上graphviz:
python version-dag.py > log.dot ; dot -Tpdf log.dot -o log.pdf
...然后看看光荣的 .pdf 文件。Graphviz 可以做 png、eps 和其他格式。该jump
字典用于将单父路径压缩到单个节点。享受 :)